|
-
May 14th, 2010, 07:21 AM
#1
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 ..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|