Error passing and getting Array values in Java
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
add a comment |
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
cost_grd[i]
is adouble
butTotal_cost()
returns adouble
(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 asnew 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
add a comment |
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
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
java arrays methods
edited Nov 15 '18 at 22:34
reymagnus
asked Nov 15 '18 at 20:55
reymagnusreymagnus
155
155
cost_grd[i]
is adouble
butTotal_cost()
returns adouble
(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 asnew 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
add a comment |
cost_grd[i]
is adouble
butTotal_cost()
returns adouble
(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 asnew 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
add a comment |
2 Answers
2
active
oldest
votes
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();
Ok, but now I get this error in code: Exception in thread "main" java.lang.NullPointerException
– reymagnus
Nov 15 '18 at 22:34
add a comment |
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();
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
Ok, but now I get this error in code: Exception in thread "main" java.lang.NullPointerException
– reymagnus
Nov 15 '18 at 22:34
add a comment |
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();
Ok, but now I get this error in code: Exception in thread "main" java.lang.NullPointerException
– reymagnus
Nov 15 '18 at 22:34
add a comment |
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();
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();
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
add a comment |
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
add a comment |
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();
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
add a comment |
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();
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
add a comment |
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();
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();
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
cost_grd[i]
is adouble
butTotal_cost()
returns adouble
(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