Error passing and getting Array values in Java










-6















How I pass an array to another array?



I need to pass an array of double values but I don't know why it doesn't work.
This is my code:



 private double Total_cost()
double cost=new double[2];
.
.
.
cost[0]=tot_cost;
cost[1]=tot_cost2;
return cost;



Calling the method:



public void estr_grdcmpabs()
//
double cost_grd=new double[2];
.
.
.
cost_grd= Total_cost();
cst = String.valueOf(cost_grd[0]);



I get the error:



Exception in thread "main" java.lang.NullPointerException



How I convert the array value to a string to show in jframe?



Thanks.










share|improve this question
























  • cost_grd[i] is a double but Total_cost() returns a double (i.e. an array), so the assignment doesn't work. What are you actually trying to do?

    – arshajii
    Nov 15 '18 at 20:58












  • I need to pass an array to an array.

    – reymagnus
    Nov 15 '18 at 21:01











  • A variable declared as new double[1] has only one element, and its index is 0. Indexing it at 1 will throw an exception.

    – Klitos Kyriacou
    Nov 15 '18 at 21:03











  • Exception in thread "main" java.lang.NullPointerException

    – reymagnus
    Nov 15 '18 at 21:11















-6















How I pass an array to another array?



I need to pass an array of double values but I don't know why it doesn't work.
This is my code:



 private double Total_cost()
double cost=new double[2];
.
.
.
cost[0]=tot_cost;
cost[1]=tot_cost2;
return cost;



Calling the method:



public void estr_grdcmpabs()
//
double cost_grd=new double[2];
.
.
.
cost_grd= Total_cost();
cst = String.valueOf(cost_grd[0]);



I get the error:



Exception in thread "main" java.lang.NullPointerException



How I convert the array value to a string to show in jframe?



Thanks.










share|improve this question
























  • cost_grd[i] is a double but Total_cost() returns a double (i.e. an array), so the assignment doesn't work. What are you actually trying to do?

    – arshajii
    Nov 15 '18 at 20:58












  • I need to pass an array to an array.

    – reymagnus
    Nov 15 '18 at 21:01











  • A variable declared as new double[1] has only one element, and its index is 0. Indexing it at 1 will throw an exception.

    – Klitos Kyriacou
    Nov 15 '18 at 21:03











  • Exception in thread "main" java.lang.NullPointerException

    – reymagnus
    Nov 15 '18 at 21:11













-6












-6








-6


0






How I pass an array to another array?



I need to pass an array of double values but I don't know why it doesn't work.
This is my code:



 private double Total_cost()
double cost=new double[2];
.
.
.
cost[0]=tot_cost;
cost[1]=tot_cost2;
return cost;



Calling the method:



public void estr_grdcmpabs()
//
double cost_grd=new double[2];
.
.
.
cost_grd= Total_cost();
cst = String.valueOf(cost_grd[0]);



I get the error:



Exception in thread "main" java.lang.NullPointerException



How I convert the array value to a string to show in jframe?



Thanks.










share|improve this question
















How I pass an array to another array?



I need to pass an array of double values but I don't know why it doesn't work.
This is my code:



 private double Total_cost()
double cost=new double[2];
.
.
.
cost[0]=tot_cost;
cost[1]=tot_cost2;
return cost;



Calling the method:



public void estr_grdcmpabs()
//
double cost_grd=new double[2];
.
.
.
cost_grd= Total_cost();
cst = String.valueOf(cost_grd[0]);



I get the error:



Exception in thread "main" java.lang.NullPointerException



How I convert the array value to a string to show in jframe?



Thanks.







java arrays methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 22:34







reymagnus

















asked Nov 15 '18 at 20:55









reymagnusreymagnus

155




155












  • cost_grd[i] is a double but Total_cost() returns a double (i.e. an array), so the assignment doesn't work. What are you actually trying to do?

    – arshajii
    Nov 15 '18 at 20:58












  • I need to pass an array to an array.

    – reymagnus
    Nov 15 '18 at 21:01











  • A variable declared as new double[1] has only one element, and its index is 0. Indexing it at 1 will throw an exception.

    – Klitos Kyriacou
    Nov 15 '18 at 21:03











  • Exception in thread "main" java.lang.NullPointerException

    – reymagnus
    Nov 15 '18 at 21:11

















  • cost_grd[i] is a double but Total_cost() returns a double (i.e. an array), so the assignment doesn't work. What are you actually trying to do?

    – arshajii
    Nov 15 '18 at 20:58












  • I need to pass an array to an array.

    – reymagnus
    Nov 15 '18 at 21:01











  • A variable declared as new double[1] has only one element, and its index is 0. Indexing it at 1 will throw an exception.

    – Klitos Kyriacou
    Nov 15 '18 at 21:03











  • Exception in thread "main" java.lang.NullPointerException

    – reymagnus
    Nov 15 '18 at 21:11
















cost_grd[i] is a double but Total_cost() returns a double (i.e. an array), so the assignment doesn't work. What are you actually trying to do?

– arshajii
Nov 15 '18 at 20:58






cost_grd[i] is a double but Total_cost() returns a double (i.e. an array), so the assignment doesn't work. What are you actually trying to do?

– arshajii
Nov 15 '18 at 20:58














I need to pass an array to an array.

– reymagnus
Nov 15 '18 at 21:01





I need to pass an array to an array.

– reymagnus
Nov 15 '18 at 21:01













A variable declared as new double[1] has only one element, and its index is 0. Indexing it at 1 will throw an exception.

– Klitos Kyriacou
Nov 15 '18 at 21:03





A variable declared as new double[1] has only one element, and its index is 0. Indexing it at 1 will throw an exception.

– Klitos Kyriacou
Nov 15 '18 at 21:03













Exception in thread "main" java.lang.NullPointerException

– reymagnus
Nov 15 '18 at 21:11





Exception in thread "main" java.lang.NullPointerException

– reymagnus
Nov 15 '18 at 21:11












2 Answers
2






active

oldest

votes


















0














The size of the cost array is 2 but you have declared it of size 1. This will create ArrayIndexOutOfBoundsException.
Replace the loop with this single statement so that the return type double matches



 cost_grd= Total_cost();





share|improve this answer























  • Ok, but now I get this error in code: Exception in thread "main" java.lang.NullPointerException

    – reymagnus
    Nov 15 '18 at 22:34



















2














Cost is an array of doubles, while cost_grd[i] is just a double element in the cost_grad array of doubles
To solve either change the statement to



cost_grd[i]= Total_cost()[i];


Or remove the loop and change to:



cost_grd= Total_cost();





share|improve this answer























  • Another error, size is one while you are trying to put two elements, just change the size of cost_grd and cost to two

    – Hossam26644
    Nov 15 '18 at 21:13










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327772%2ferror-passing-and-getting-array-values-in-java%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The size of the cost array is 2 but you have declared it of size 1. This will create ArrayIndexOutOfBoundsException.
Replace the loop with this single statement so that the return type double matches



 cost_grd= Total_cost();





share|improve this answer























  • Ok, but now I get this error in code: Exception in thread "main" java.lang.NullPointerException

    – reymagnus
    Nov 15 '18 at 22:34
















0














The size of the cost array is 2 but you have declared it of size 1. This will create ArrayIndexOutOfBoundsException.
Replace the loop with this single statement so that the return type double matches



 cost_grd= Total_cost();





share|improve this answer























  • Ok, but now I get this error in code: Exception in thread "main" java.lang.NullPointerException

    – reymagnus
    Nov 15 '18 at 22:34














0












0








0







The size of the cost array is 2 but you have declared it of size 1. This will create ArrayIndexOutOfBoundsException.
Replace the loop with this single statement so that the return type double matches



 cost_grd= Total_cost();





share|improve this answer













The size of the cost array is 2 but you have declared it of size 1. This will create ArrayIndexOutOfBoundsException.
Replace the loop with this single statement so that the return type double matches



 cost_grd= Total_cost();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 21:13









AMOGH KALYANSHETTIAMOGH KALYANSHETTI

583




583












  • Ok, but now I get this error in code: Exception in thread "main" java.lang.NullPointerException

    – reymagnus
    Nov 15 '18 at 22:34


















  • Ok, but now I get this error in code: Exception in thread "main" java.lang.NullPointerException

    – reymagnus
    Nov 15 '18 at 22:34

















Ok, but now I get this error in code: Exception in thread "main" java.lang.NullPointerException

– reymagnus
Nov 15 '18 at 22:34






Ok, but now I get this error in code: Exception in thread "main" java.lang.NullPointerException

– reymagnus
Nov 15 '18 at 22:34














2














Cost is an array of doubles, while cost_grd[i] is just a double element in the cost_grad array of doubles
To solve either change the statement to



cost_grd[i]= Total_cost()[i];


Or remove the loop and change to:



cost_grd= Total_cost();





share|improve this answer























  • Another error, size is one while you are trying to put two elements, just change the size of cost_grd and cost to two

    – Hossam26644
    Nov 15 '18 at 21:13















2














Cost is an array of doubles, while cost_grd[i] is just a double element in the cost_grad array of doubles
To solve either change the statement to



cost_grd[i]= Total_cost()[i];


Or remove the loop and change to:



cost_grd= Total_cost();





share|improve this answer























  • Another error, size is one while you are trying to put two elements, just change the size of cost_grd and cost to two

    – Hossam26644
    Nov 15 '18 at 21:13













2












2








2







Cost is an array of doubles, while cost_grd[i] is just a double element in the cost_grad array of doubles
To solve either change the statement to



cost_grd[i]= Total_cost()[i];


Or remove the loop and change to:



cost_grd= Total_cost();





share|improve this answer













Cost is an array of doubles, while cost_grd[i] is just a double element in the cost_grad array of doubles
To solve either change the statement to



cost_grd[i]= Total_cost()[i];


Or remove the loop and change to:



cost_grd= Total_cost();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 20:59









Hossam26644Hossam26644

566




566












  • Another error, size is one while you are trying to put two elements, just change the size of cost_grd and cost to two

    – Hossam26644
    Nov 15 '18 at 21:13

















  • Another error, size is one while you are trying to put two elements, just change the size of cost_grd and cost to two

    – Hossam26644
    Nov 15 '18 at 21:13
















Another error, size is one while you are trying to put two elements, just change the size of cost_grd and cost to two

– Hossam26644
Nov 15 '18 at 21:13





Another error, size is one while you are trying to put two elements, just change the size of cost_grd and cost to two

– Hossam26644
Nov 15 '18 at 21:13

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327772%2ferror-passing-and-getting-array-values-in-java%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線