Results 1 to 14 of 14
 05262010, 08: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, 08:54 AM #2Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 13
 05262010, 09:24 AM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
 05262010, 09:47 AM #4Member
 Join Date
 May 2010
 Posts
 4
 Rep Power
 0
 05262010, 10:07 AM #5Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 13
Do yourself a favour and read the link posted in the first reply.
 05262010, 11:33 AM #6Senior Member
 Join Date
 Apr 2010
 Location
 Belgrade, Serbia
 Posts
 278
 Rep Power
 10
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, 11:33 AM #7Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 26
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, 06:53 AM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
 05272010, 06:55 AM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
 06012010, 04:07 AM #10Member
 Join Date
 May 2010
 Posts
 4
 Rep Power
 0
thanks everyone. I figure it out.
 06022010, 01:42 PM #11
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
If you've solve the problem, please mark the thread solved.
 06022010, 01:45 PM #12
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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, 02:27 PM #13Member
 Join Date
 May 2010
 Posts
 4
 Rep Power
 0
Thank you, I will pay attention to my format next time
 06022010, 02:56 PM #14
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Similar Threads

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