getting nullpoint exception again

Reply

Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

getting nullpoint exception again

 
0
  #1
Jul 5th, 2009
  1.  
  2. not clear with the basics of nullpont error; trying to create student database
  3.  
  4. import java.io.*;
  5. class student1
  6. {
  7. String name;
  8. int id;
  9. public void getdata(String name1,int id1)
  10. {
  11. name=name1;
  12. id=id1;
  13. }
  14. public void putdata()
  15. {
  16. System.out.println("\t"+id+" "+name);
  17. }
  18. }
  19. class student
  20. {
  21. public static void main(String args[])throws IOException
  22. {
  23. BufferedReader br= new BufferedReader(new
  24.  
  25. InputStreamReader(System.in));
  26. BufferedReader br1= new BufferedReader(new
  27.  
  28. InputStreamReader(System.in));
  29. student1[] n=new student1[10];
  30. for(int i=0;i<5;i++)
  31. {
  32. n[i].getdata(br.readLine(),Integer.parseInt(br1.readLine()));
  33. n[i].putdata();
  34.  
  35. }
  36. }
  37. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: getting nullpoint exception again

 
0
  #2
Jul 5th, 2009
The exception contains a line number, so which line is it?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: getting nullpoint exception again

 
0
  #3
Jul 5th, 2009
  1.  
  2. java:29 line 29
  3. thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: getting nullpoint exception again

 
0
  #4
Jul 5th, 2009
And which line there is line 29?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: getting nullpoint exception again

 
0
  #5
Jul 5th, 2009
  1.  
  2. n[i].getdata(....)
  3. tried without using BufferedReader directly putting values but same error.Object seems to be null.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 965
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 144
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: getting nullpoint exception again

 
1
  #6
Jul 5th, 2009
n[] is new array, has nothing in it. So n[i] has nothing in it for all values of i, so n[i].someFunction tries to call someFunction on a null value -> Exception.
Maybe initialise n[i] to new Student1() before using it?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: getting nullpoint exception again

 
0
  #7
Jul 5th, 2009
  1.  
  2. thanks a lot a bunch it worked
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: getting nullpoint exception again

 
0
  #8
Jul 5th, 2009
  1.  
  2. Dear James
  3. i wished you could solve this NPE too. i am trying to separate the words and print the longest. i tried using s.length but found difficult
  4. getting error at toCompare which compares the length of strings
  5.  
  6. class maxstring1
  7. {
  8. public static void main(String args[])
  9. {
  10. String s1="nikhil is a good boy";
  11. int d;int x=0;
  12. d=s1.length();
  13. String[] s2=new String[20];
  14. char ch;
  15. for(int i=0;i<=d-1;i++)
  16. {
  17. ch=s1.charAt(i);
  18. if(ch==' ')
  19. {
  20. s2[i]=new String();
  21. s2[i]=s1.substring(x,i-1);
  22. x=i+1;
  23. }
  24. }
  25. for(int i=0;i<=d-1;i++)
  26. {
  27. s2[i]=new String();
  28. System.out.println(s2[i]);
  29. if(s2[i].compareTo(max)>0)
  30. max=s2[i];
  31. }
  32. System.out.println(max);
  33. }
  34. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 965
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 144
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: getting nullpoint exception again

 
0
  #9
Jul 5th, 2009
NPE is probably because max isn't defined or initialised. But you have other problems.eg: S2 is initialised to empty Strings, but you never put anything else in there before comparing them. And you compare them alphabetically, not by length.
Have a look at the split(" ") method for Strings - this will get you your words just like you need them...

EDIT: I just saw that you have already posted this problem, and a number of other posters have given the same advice already, and you don't seem to have followed it. Are you wasting my time?
Last edited by JamesCherrill; Jul 5th, 2009 at 1:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: getting nullpoint exception again

 
0
  #10
Jul 5th, 2009
[code]

i am sorry

i wanted to do it in my way. I know and remember other posts.But if you could solve the NPE problem in one case u could do it in other as well. I know the split or String tokenizer method. (i am tryng to explain for loop to one of my friend so i just took a chance if u could solve it.But its ok.
sorry again .

thanks
akulkarni
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC