Results 1 to 14 of 14
- 05-26-2010, 07:17 AM #1
Member
- 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;
}
- 05-26-2010, 07:54 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 05-26-2010, 08:24 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-26-2010, 08:47 AM #4
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
- 05-26-2010, 09:07 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Do yourself a favour and read the link posted in the first reply.
- 05-26-2010, 10:33 AM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
try to initialize object a.Java Code:a.matrix[p][q]=stok.nval; // the exception occurs at this line.
For example:
Java Code:Matrix[][] a = new Matrix[p][q];
- 05-26-2010, 10:33 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 05-27-2010, 05:53 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-27-2010, 05:55 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-01-2010, 03:07 AM #10
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
thanks everyone. I figure it out.
- 06-02-2010, 12:42 PM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you've solve the problem, please mark the thread solved.
- 06-02-2010, 12:45 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-02-2010, 01:27 PM #13
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Thank you, I will pay attention to my format next time
- 06-02-2010, 01:56 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Complete Java Removal
By jpChris in forum New To JavaReplies: 14Last Post: 05-25-2010, 12:46 PM -
ejb 3.0 complete tutorial
By better_me007 in forum Enterprise JavaBeans (EJB)Replies: 3Last Post: 09-28-2009, 10:52 AM -
Help me to complete this class, please
By pink_872 in forum New To JavaReplies: 6Last Post: 04-21-2009, 01:23 PM -
Download the Complete API?
By JDCAce in forum NetBeansReplies: 2Last Post: 10-02-2008, 01:02 AM -
Please complete the setRate mthod
By Adiel224 in forum New To JavaReplies: 6Last Post: 09-19-2008, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks