DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   getting nullpoint exception again (http://www.daniweb.com/forums/thread201778.html)

akulkarni Jul 5th, 2009 9:33 am
getting nullpoint exception again
 

not clear with the basics of nullpont error; trying to create student database

import java.io.*;
class student1
{
String name;
int id;
public void getdata(String name1,int id1)
{
name=name1;
id=id1;
}
public void putdata()
{
System.out.println("\t"+id+" "+name);
}
}
class student
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new

InputStreamReader(System.in));
BufferedReader br1= new BufferedReader(new

InputStreamReader(System.in));
student1[] n=new student1[10];
for(int i=0;i<5;i++)
{
n[i].getdata(br.readLine(),Integer.parseInt(br1.readLine()));
n[i].putdata();

}
}
}

masijade Jul 5th, 2009 10:17 am
Re: getting nullpoint exception again
 
The exception contains a line number, so which line is it?

akulkarni Jul 5th, 2009 12:18 pm
Re: getting nullpoint exception again
 

java:29 line 29
thanks

masijade Jul 5th, 2009 12:31 pm
Re: getting nullpoint exception again
 
And which line there is line 29?

akulkarni Jul 5th, 2009 12:41 pm
Re: getting nullpoint exception again
 

n[i].getdata(....)
tried without using BufferedReader directly putting values but same error.Object seems to be null.

JamesCherrill Jul 5th, 2009 12:47 pm
Re: getting nullpoint exception again
 
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?

akulkarni Jul 5th, 2009 12:58 pm
Re: getting nullpoint exception again
 

thanks a lot a bunch it worked

akulkarni Jul 5th, 2009 1:27 pm
Re: getting nullpoint exception again
 

Dear James
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
getting error at toCompare which compares the length of strings

class maxstring1
{
public static void main(String args[])
{
String s1="nikhil is a good boy";
int d;int x=0;
d=s1.length();
String[] s2=new String[20];
char ch;
for(int i=0;i<=d-1;i++)
{
ch=s1.charAt(i);
if(ch==' ')
{
s2[i]=new String();
s2[i]=s1.substring(x,i-1);
x=i+1;
}
}
for(int i=0;i<=d-1;i++)
{
s2[i]=new String();
System.out.println(s2[i]);
if(s2[i].compareTo(max)>0)
max=s2[i];
}
System.out.println(max);
}
}

JamesCherrill Jul 5th, 2009 1:39 pm
Re: getting nullpoint exception again
 
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?

akulkarni Jul 5th, 2009 1:58 pm
Re: getting nullpoint exception again
 
[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


All times are GMT -4. The time now is 6:01 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC