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();

}
}
}

Recommended Answers

All 11 Replies

The exception contains a line number, so which line is it?

java:29 line 29
thanks

And which line there is line 29?

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

n[] is new array, has nothing in it. So n has nothing in it for all values of i, so n.someFunction tries to call someFunction on a null value -> Exception.
Maybe initialise n to new Student1() before using it?

commented: excellent for a newbie like me +1
thanks a lot a bunch it worked
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);
}
}

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?

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

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 friends so i just took a chance if u could solve it.But its ok.
sorry again .

thanks
akulkarni

OK. But you already have the explanation of this NPE, and just patching the code to bypass it still won't make your program work.
If you really don't want to use split, you should have program that looks like this:

create empty string array for words.
start copying input to a temporary string until you hit a " "
put the temp string into the first/next element of word array
repeat until input exhausted.

Test, and don't try to do anything else until this works.

Create temp String "" to hold longest.
Compare length of each word against length of the temp String
if it's longer, copy word to the temp String

When all words processed, temp String contains the longest one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.