Results 1 to 7 of 7

Thread: I am confused

  1. #1
    prof.deedee is offline Member
    Join Date
    Oct 2009
    Posts
    36
    Rep Power
    0

    Default 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:

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Java Code:
    public static node[] state = new node[4];
    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:
    node[0] = new Node();
    node[1] = new Node();
    node[2] = new Node();
    node[3] = new Node();
    Last edited by mrmatt1111; 10-30-2009 at 10:47 PM.
    My Hobby Project: LegacyClone

  3. #3
    prof.deedee is offline Member
    Join Date
    Oct 2009
    Posts
    36
    Rep Power
    0

    Default

    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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    prof.deedee is offline Member
    Join Date
    Oct 2009
    Posts
    36
    Rep Power
    0

    Default

    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();

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.
    Java Code:
    for (int i = 0; i < state.length; i++) {
      state[i] = new Node();  // again, the Node class should be capitalized.  
    }
    This could be done in the main method, at the top.

    Much luck.
    Last edited by Fubarable; 10-31-2009 at 12:06 AM.

  7. #7
    prof.deedee is offline Member
    Join Date
    Oct 2009
    Posts
    36
    Rep Power
    0

Similar Threads

  1. Very confused Plz help!!
    By ratb0y in forum NetBeans
    Replies: 0
    Last Post: 02-14-2009, 05:34 PM
  2. Confused
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 01-13-2009, 02:00 PM
  3. confused
    By updev in forum AWT / Swing
    Replies: 6
    Last Post: 11-14-2008, 04:33 PM
  4. a lot confused
    By vineethraj in forum New To Java
    Replies: 4
    Last Post: 01-18-2008, 01:36 AM
  5. what does it mean:confused:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 05:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •