trouble with making a simple construct for an array of double type
Here is my partial code:
public class Variable {
private double[] array;
public Variable(){
array[0] = 0.0;
}
public String toString(){
return array.toString();
}
public static void main(String[] args) {
Variable Var1 = new Variable();
System.out.println(Var1);
}
And I want to get the following output to simply be:
[0.0]
but for some reason there is still error. What am I doing wrong?
Re: trouble with making a simple construct for an array of double type
Quote:
for some reason there is still error
Please copy and paste here the full text of the error messages.
Re: trouble with making a simple construct for an array of double type
it just keeps prompting me to go to Debug mode, and does not provide a way to correct it, but i know that the error lies in
"public Variable(){
array[0] = 0.0;
}"
b/c that's where the debug first starts
Re: trouble with making a simple construct for an array of double type
Try to compile the program to get a compiler error message. Copy and paste the full text of the compiler error message here.
It's hard to suggest a fix without knowing what the problem is.
Please Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: trouble with making a simple construct for an array of double type
well, i compiled the code many times, and there were no error messages while compiling; only while running.
While running, however, it says that their is a NullPointerException on
Re: trouble with making a simple construct for an array of double type
Quote:
NullPointerException
Where do you assign a value to the array variable? It will have a null value if it is not assigned a value.
Re: trouble with making a simple construct for an array of double type
Here is the code again. Does this make it easier to figure out?
Code java:
public class Variable {
private double[] array;
public Variable(){
array[0] = 0.0;
}
public String toString(){
return array.toString();
}
public static void main(String[] args) {
Variable Var1 = new Variable();
System.out.println(Var1);
}
Re: trouble with making a simple construct for an array of double type
Where does the code assign a value to array? You need to add code that assigns a value to array.
Take a look at the tutorial:
http://docs.oracle.com/javase/tutori...ts/arrays.html
Re: trouble with making a simple construct for an array of double type
ohhhhhhhhhhhhhhhhhhhhh I see what I did. i know it was a noob mistake, but thanks!!
--- Update ---
Now how would you be able to convert an array into a string?
Re: trouble with making a simple construct for an array of double type
Please give an example of an array and the String you want to create from it.
The String class has methods that can help you.