Results 1 to 14 of 14
  1. #1
    keye906 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default can anyone help me,a complete newbie?

    I have just started to learn java.

    these days, I am always bothered by NullpointerException.

    I try to pass a object to a method of its own class, but whenever

    I want to read the data of that object or try to modify the content

    of the object, I always get a NullpointerException. I don't know why this

    happens. can some kind veteran give me a hand? here is an code encounter

    this problem:

    I first defined a class called matrix which mainly consist of a 2D array, "enter" is a method of this class that receive a matrix object and return a matrix object.

    in main method, I first instantiate an object of the matrix class and then
    pass it to the "enter " method.

    matrix h=new matrix(i,j);
    h=h.enter(h);

    public matrix enter(matrix a) // "enter method"
    {
    try{
    for(p=0;p<a.row;p++)
    {

    StreamTokenizer stok=new StreamTokenizer(br);
    q=0;
    while(stok.nextToken() !=';' )
    {
    a.matrix[p][q]=stok.nval; // the exception occurs at this line.
    q++;
    }
    }

    for(p=0;p<a.col;p++)
    {
    for(q=0;q<a.col;q++)
    {
    System.out.print(a.matrix[p][q]+ " ");
    }
    System.out.println();
    }
    } catch (IOException e) {throw e;}
    return a;
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by keye906 View Post
    I always get a NullpointerException. I don't know why this happens.
    Because at runtime, one of variable not assign with the correct/valid value.

  4. #4
    keye906 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Because at runtime, one of variable not assign with the correct/valid value.
    do you mean a object was not properly instantiated, the problem is about this method?

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Do yourself a favour and read the link posted in the first reply.

  6. #6
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
    a.matrix[p][q]=stok.nval; // the exception occurs at this line.
    try to initialize object a.

    For example:
    Java Code:
    Matrix[][] a = new Matrix[p][q];

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    a.matrix[p][q]=stok.nval;

    Either a is null, a.matrix is null, a.matix[p] is null or stok is null.

    Now, since a.row() a couple of lines earlier didn't throw an NPE, and stock.hasNext() didn't either, these are not null.

    So that leaves the matrix attribute in your matrix class (and classes should start with a capital, so that would be Matrix), or part of its array.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by keye906 View Post
    do you mean a object was not properly instantiated, the problem is about this method?
    Yes. Initialized with a null value.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    a.matrix[p][q]=stok.nval;

    Either a is null, a.matrix is null, a.matix[p] is null or stok is null.
    Check this out.

  10. #10
    keye906 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    thanks everyone. I figure it out.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also please use code tags when you post code segments in the forum next time. Unformated codes are really hard to read. If you don't know how to do it, check on my forum signature. You can find the relevant link.

  13. #13
    keye906 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank you, I will pay attention to my format next time

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Complete Java Removal
    By jpChris in forum New To Java
    Replies: 14
    Last Post: 05-25-2010, 12:46 PM
  2. ejb 3.0 complete tutorial
    By better_me007 in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 09-28-2009, 10:52 AM
  3. Help me to complete this class, please
    By pink_872 in forum New To Java
    Replies: 6
    Last Post: 04-21-2009, 01:23 PM
  4. Download the Complete API?
    By JDCAce in forum NetBeans
    Replies: 2
    Last Post: 10-02-2008, 01:02 AM
  5. Please complete the setRate mthod
    By Adiel224 in forum New To Java
    Replies: 6
    Last Post: 09-19-2008, 10:41 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
  •