Results 1 to 14 of 14
 05262010, 07:17 AM #1Member
 Join Date
 May 2010
 Posts
 4
 Rep Power
 0
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;
}
 05262010, 07:54 AM #2Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 14
 05262010, 08:24 AM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 26
 05262010, 08:47 AM #4Member
 Join Date
 May 2010
 Posts
 4
 Rep Power
 0
 05262010, 09:07 AM #5Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 14
Do yourself a favour and read the link posted in the first reply.
 05262010, 10:33 AM #6Senior Member
 Join Date
 Apr 2010
 Location
 Belgrade, Serbia
 Posts
 278
 Rep Power
 11
Java Code:a.matrix[p][q]=stok.nval; // the exception occurs at this line.
For example:
Java Code:Matrix[][] a = new Matrix[p][q];
 05262010, 10:33 AM #7Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 27
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.
 05272010, 05:53 AM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 26
 05272010, 05:55 AM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 26
 06012010, 03:07 AM #10Member
 Join Date
 May 2010
 Posts
 4
 Rep Power
 0
thanks everyone. I figure it out.
 06022010, 12:42 PM #11
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 26
If you've solve the problem, please mark the thread solved.
 06022010, 12:45 PM #12
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 26
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.
 06022010, 01:27 PM #13Member
 Join Date
 May 2010
 Posts
 4
 Rep Power
 0
Thank you, I will pay attention to my format next time
 06022010, 01:56 PM #14
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 26
Similar Threads

Complete Java Removal
By jpChris in forum New To JavaReplies: 14Last Post: 05252010, 12:46 PM 
ejb 3.0 complete tutorial
By better_me007 in forum Enterprise JavaBeans (EJB)Replies: 3Last Post: 09282009, 10:52 AM 
Help me to complete this class, please
By pink_872 in forum New To JavaReplies: 6Last Post: 04212009, 01:23 PM 
Download the Complete API?
By JDCAce in forum NetBeansReplies: 2Last Post: 10022008, 01:02 AM 
Please complete the setRate mthod
By Adiel224 in forum New To JavaReplies: 6Last Post: 09192008, 10:41 AM
Bookmarks