Exception in thread "main" java.lang.NullPointerException ???
HI ALL ..
I'm Having this problem in my program ..
The OutPut is :
Code:
(1) Open New File (2) Open Existing File
Enter number of choice you want:2
Enter the name: N1
Exception in thread "main" java.lang.NullPointerException
at P.search(P.java:143)
at P.searchName(P.java:122)
at P.<init>(P.java:24)
at CL.main(CL.java:26)
Process completed.
When I open a new file,everyting is correct .. Just for open existing file Give me this ERROR =(
How To Solve it ??
This is the main Class :
Code:
import java.util.Scanner;
public class CL
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
String name=null;
String gender=null;
String date=null;
System.out.print("\n(1) Open New File \t(2) Open Existing File\n\n");
System.out.print("Enter number of choice you want:");
int x=input.nextInt();
if(x==1)
{
P obj1= new P (name,gender,date);
System.out.printf("\n\n%s\t\t%s\t\t%s\n%s\t\t\t\t%s\t\t\t%s\n\n","Patient Name: ","Gender: ","Date: ",obj1.getPatientName(),obj1.getGender(),obj1.getDate());
}
if (x==2)
{
System.out.print("\nEnter the name: ");
String N = input.next();
P obj2= new P (N);
}
}
}
The Second Class :
Code:
import java.util.Scanner;
public class P
{
private String[] patientName={null,"N1",null,null,"N2",null,"N3",null,null,"N4"};
private String[] Gender={null,"F",null,null,"M",null,"M",null,null,"F"};
String name;
String gender;
private int Month;
private int Day;
private int Year;
Scanner input=new Scanner(System.in);
public P (String name , String gender, String date)
{
setPatientName(name);
setGender (gender);
setDate(Month,Day,Year);
}
public P (String name )
{
searchName (name);
}
public void setPatientName(String name)
{
int i;
System.out.print("\nEnter the name: ");
name = input.next();
for(i=0; i<patientName.length; i++)
{
patientName[i]=name;
break;
}
}
public String getPatientName ()
{
int i=0;
return patientName[i];
}
public void setGender ( String gender)
{
int i;
System.out.print("\nEnter the gender: ");
gender = input.next();
for(i=0;i<Gender.length;i++)
{
Gender[i]=gender;
break;
}
}
public String getGender()
{
int i=0;
return Gender[i];
}
public void setDate(int month , int day , int year )
{
Month = checkMonth( month );
setYear (year);
Day = checkDay( day );
}
public String getDate()
{
return String.format( "%d/%d/%d", Day , Month , Year );
}
public int checkMonth( int month )
{
System.out.print("\nEnter the month: ");
month = input.nextInt();
if ( month > 0 && month <= 12 )
return month;
else
{
System.out.printf("Invalid month (%d) set to 1.", month );
return 1;
}
}
public int checkDay( int day )
{
System.out.print("\nEnter the day: ");
day = input.nextInt();
int daysPerMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ( day > 0 && day <= daysPerMonth[ Month ] )
return day;
if ( Month == 2 && day == 29 && ( Year % 400 == 0 ||
( Year % 4 == 0 && Year % 100 != 0 ) ) )
return day;
System.out.printf( "Invalid day (%d) set to 1.", day );
return 1;
}
public void setYear (int year)
{ System.out.print("\nEnter the year: ");
year = input.nextInt();
Year = year;
}
public int getYear()
{
return Year;
}
public void searchName(String name)
{
int result=search(name);
if (result!=-1)
{
System.out.println("Registered Patient");
System.out.printf("\n\n%s\t\t%s\n%s\t\t\t\t%s\n\n","Patient Name: ","Gender: ",patientName[result],Gender[result]);
}
else
{
System.out.println("You are not registered");
setGender (gender);
setDate(Month,Day,Year);
System.out.printf("\n\n%s\t\t%s\t\t%s\n%s\t\t\t\t%s\t\t\t%s\n\n","Patient Name: ","Gender: ","Date: ",name,gender,getDate());
}
}
public int search(String name)
{int a=0;
for ( int j=0; j < patientName.length; j++ )
{
if ( patientName[j].equals(name) )
a = j ; // found
else
a = -1 ; //not found.
}
return a ;
}
}
Any Help is Geatly Appreciated ..
Re: Exception in thread "main" java.lang.NullPointerException ???
Based off of the error message, and a quick look at your code, the error appears to be thrown here:
Quote:
if ( patientName[j].equals(name))
In the array patientName, you have null values:
Quote:
private String[] patientName={null,"N1",null,null,"N2",null,"N3",null,null,"N4"};
You cannot perform the following operation:
Quote:
if( null.equals(someObject))
This will throw a, wait for it... a NullPointerException. You cannot try to use members of some object if the object is null. You are trying to use .equals on null and you simply can't do that.
Re: Exception in thread "main" java.lang.NullPointerException ???
THX ALOT ProgramThis for your explainitaion ..
I removed null values ..
but it still tell me you are not registered .. =(
Code:
(1) Open New File (2) Open Existing File
Enter number of choice you want:2
Enter the name: N1
You are not registered
PLZ HELP .. :S
Re: Exception in thread "main" java.lang.NullPointerException ???
Your problem is in the search method. When you find a match you are setting 'a' to the index and then continuing with the search. The following iterations fail and set 'a' to -1. You need to break out of the loop once you have found a match. You could just code it as follows:
Code:
public int search(String name)
{
for ( int j=0; j < patientName.length; j++ )
{
if ( patientName[j].equals(name) )
return j ; // found
}
return -1;
}
Re: Exception in thread "main" java.lang.NullPointerException ???
Be aware that this is cross-posted in Java Programming Forums.
Bad code isn't bad, its just misunderstood...
Anon.
Re: Exception in thread "main" java.lang.NullPointerException ???
Quote:
Be aware that this is cross-posted in Java Programming Forums.
Not another one, I'm beginning to wonder why I spend so much time replying to posts when people seem to think it's ok to cross post.