Results 1 to 7 of 7
Thread: I am confused
- 10-30-2009, 09:41 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 36
- Rep Power
- 0
I am confused
Hi
When I wrote this code:
public class Main {
public class node {
int[] nod = {0, 0, 0};
}
public static node[] state = new node[4];
public static void divide(int[] M )
{
state[0].nod[0] = M[0];
state[0].nod[1] = M[1];
state[0].nod[2] = M[2];
state[1].nod[0] = M[3];
state[1].nod[1] = M[4];
state[1].nod[2] = M[5];
state[2].nod[0] = M[6];
state[2].nod[1] = M[7];
state[2].nod[2] = M[8];
state[3].nod[0] = M[9];
state[3].nod[1] = M[10];
state[3].nod[2] = M[11];
}
public static void main(String[] args) {
int[] M = {0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0};
divide(M);
}
}
there is no error but when I try run it this message appear::eek::eek:
Exception in thread "main" java.lang.NullPointerException
at cry.Main.divide(Main.java:21)
at cry.Main.main(Main.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
what it means ???:confused::confused:
- 10-30-2009, 09:44 PM #2
creates an array of size 4, but you still need to call the constructor and create the actual object for each of those four.Java Code:public static node[] state = new node[4];
Java Code:node[0] = new Node(); node[1] = new Node(); node[2] = new Node(); node[3] = new Node();
Last edited by mrmatt1111; 10-30-2009 at 09:47 PM.
My Hobby Project: LegacyClone
- 10-30-2009, 10:28 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 36
- Rep Power
- 0
I can't understand u..could you please explain more..
first I create object node which contain array of integer nod this array length is 3
then I create array from that object node its name state and this array length is 4
so where should I put the constructor ..
thanks
-
.... um, he showed you where to put the calls to the Node constructor.
- 10-30-2009, 10:56 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 36
- Rep Power
- 0
sorry sir but I am new in Java so can you tell me exactly where I should but that call
and I don't have Node type it's node
did you mean
state[0]=new node();
-
It should be Node as class names should begin with a capital letter. I recommend you change it.
After you declare your array of four Nodes, but before you use them, use a for loop to loop through the array and initialize each Node in the array by calling the Node constructor.
This could be done in the main method, at the top.Java Code:for (int i = 0; i < state.length; i++) { state[i] = new Node(); // again, the Node class should be capitalized. }
Much luck.Last edited by Fubarable; 10-30-2009 at 11:06 PM.
- 10-30-2009, 11:32 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
Very confused Plz help!!
By ratb0y in forum NetBeansReplies: 0Last Post: 02-14-2009, 04:34 PM -
Confused
By coldfire in forum New To JavaReplies: 3Last Post: 01-13-2009, 01:00 PM -
confused
By updev in forum AWT / SwingReplies: 6Last Post: 11-14-2008, 03:33 PM -
a lot confused
By vineethraj in forum New To JavaReplies: 4Last Post: 01-18-2008, 12:36 AM -
what does it mean:confused:
By sivasayanth in forum New To JavaReplies: 2Last Post: 01-12-2008, 04:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks