Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: NULL pointer exception in main ...PLEASE try solving it

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default NULL pointer exception in main ...PLEASE try solving it

    ADMIN CLASS
    /////////////////////////////////////////////////////////////////////////////////////

    import java.util.Scanner;
    import java.util.Scanner;
    import java.util.Formatter;
    import java.io.*;
    public class Admin extends iiui
    implements adm
    {
    Admin()
    {
    super();
    i2=new Admin[100];


    }
    public boolean login(String u_name,String password)
    {
    int ans;
    if(password.equals("12345")&&u_name.equals("admin" ))
    return true;

    else
    return false;
    }
    public void enter_profile()
    {
    i2[i+1]= new Admin();
    Scanner input=new Scanner(System.in);
    System.out.print("\n \n\t\t ENTER NAME : ");
    i2[i+1].name=input.next();

    System.out.print("\n \n\t\tENTER REG # : ");
    i2[i+1].reg_no=input.nextInt();

    System.out.print("\n \n\t\tENTER FATHER NAME : ");
    i2[i+1].f_name=input.next();
    System.out.println("\n \t\tENTER SEMESTER : ");
    i2[i+1].semester =input.nextInt();
    System.out.print("\n \n\t\tENTER AGE : ");
    i2[i+1].age=input.nextInt();
    System.out.print("\n \n\t\tENTER NATIONALITY : ");
    i2[i+1].nationality=input.next();
    System.out.print("\n \n\t\tENTER CGPA : ");
    i2[i+1].cgpa=input.nextDouble();
    System.out.print("\n \n\t\tENTER GRADE : ");
    i2[i+1].Grade=input.next();
    System.out.print("\n \n\t\tENTER PERCENTAGE : ");
    i2[i+1].persentage=input.nextDouble();
    i++;
    }
    public void view_profile()
    {
    String name;
    int reg;
    int a,count;
    int t=0;

    System.out.println("\n \t\t1:View By Name");
    System.out.println("\n \t\t2:View By Registration number");
    System.out.print("\n \t\tOPTION : ");
    Scanner input=new Scanner(System.in);
    a=input.nextInt();
    switch(a){
    case 1:
    t=0;
    System.out.print("\n \n\t\tENTER NAME : ");
    name=input.next();
    for(count=0 ; count<=i ; count++)
    {
    a=0;
    if(i2[count].name.equals(name))
    {
    t++;
    a=count;
    System.out.print("\n \t\tRegistration number :"+i2[a].reg_no);
    System.out.print("\n \t\tName : "+i2[a].name);
    System.out.print("\n \t\tFather name : "+i2[a].f_name);
    System.out.print("\n \t\tSemester : "+i2[a].semester);
    System.out.print("\n \t\tAge : "+i2[a].age);
    System.out.print("\n \t\tGrade : "+i2[a].Grade);
    System.out.print("\n \t\tcgpa : "+i2[a].cgpa);
    System.out.print("\n \t\tPercentage : "+i2[a].persentage);
    }
    }
    if(t==0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }

    break;
    case 2:
    t=0;
    System.out.print("\n \t\tENTER REG #");
    reg=input.nextInt();
    for(count=0 ;count<=i ;count++)
    {
    a=0;
    if(i2[count].reg_no==reg)
    {
    t++;
    a=count;
    System.out.print("\n \t\tRegistration number :"+i2[a].reg_no);
    System.out.print("\n \t\tName : "+i2[a].name);
    System.out.print("\n \t\tFather name : "+i2[a].f_name);
    System.out.print("\n \t\tSemester : "+i2[a].semester);
    System.out.print("\n \t\tAge : "+i2[a].age);
    System.out.print("\n \t\tGrade : "+i2[a].Grade);
    System.out.print("\n \t\tcgpa : "+i2[a].cgpa);
    System.out.print("\n \t\tPercentage : "+i2[a].persentage);

    }
    }
    if(t==0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    break;
    default :
    System.out.println("\n \t\tWrong input");
    }

    }
    public void edit_profile()
    {
    int t=0;
    String name,name2,grade;
    int regno,age1,semester1,choice=0,ans;
    double gpa,persentage1;
    Scanner input=new Scanner(System.in);

    System.out.println("\n \t\t1:Edit by Name");
    System.out.println("\n \t\t2:Edit by Registration number");
    System.out.print("\n \t\tOPTION:");
    ans=input.nextInt();
    switch(ans)
    {
    case 1:
    t=0;
    System.out.print("\n \t\tENTER NAME: ");
    name=input.next();
    for(int j=0 ;j<=i ;j++)
    {
    int a=0;
    if(i2[j].name.equals(name))
    {
    t++;
    a=j;
    System.out.print("\n \t\tEnter New Registration number: ");
    i2[a].reg_no=input.nextInt();
    System.out.print("\n \t\tEnter New Name : ");
    i2[a].name =input.next();
    System.out.print("\n \t\tEnter New Father name : ");
    i2[a].f_name =input.next();
    System.out.print("\n \t\tEnter New Age: ");
    i2[a].age =input.nextInt();
    System.out.print("\n \t\tEnter New Semester : ");
    i2[a].semester =input.nextInt();
    System.out.print("\n \t\tEnter New Grade : ");
    i2[a].Grade =input.next();
    System.out.print("\n \t\tEnter New cgpa: ");
    i2[a].cgpa =input.nextDouble();
    System.out.print("\n \t\tEnter New Percentage : ");
    i2[a].persentage =input.nextDouble();
    }
    }
    if(t==0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    break;
    case 2:
    t=0;
    System.out.println("\n \t\tENTER REG # : ");
    regno=input.nextInt();
    for(int j=0 ;j<=i ;j++)
    {
    if(i2[j].reg_no==regno)
    {
    t++;
    int a=j;
    System.out.println("\n \t\tEnter New Registration number");
    i2[a].reg_no=input.nextInt();
    System.out.println("\n \t\tEnter New Name");
    i2[a].name =input.next();
    System.out.println("\n \t\tEnter New Father name");
    i2[a].f_name =input.next();
    System.out.println("\n \t\tEnter New Age");
    i2[a].age =input.nextInt();
    System.out.println("\n \t\tEnter New Semester");
    i2[a].semester =input.nextInt();
    System.out.println("\n \t\tEnter New Grade");
    i2[a].Grade =input.next();
    System.out.println("\n \t\tEnter New cgpa");
    i2[a].cgpa =input.nextDouble();
    System.out.println("\n \t\tEnter New Percentage");
    i2[a].persentage =input.nextDouble();

    }
    }
    if(t==0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }

    break;
    }
    }
    public void enter_result()
    {
    i2[i+1]= new Admin();
    Scanner input=new Scanner(System.in);
    System.out.print("\n \t\tENTER NAME : ");
    i2[i+1].name=input.next();
    System.out.print("\n \t\tENTER REG # : ");
    i2[i+1].reg_no=input.nextInt();
    System.out.print("\n \t\tENTER CGPA : ");
    i2[i+1].cgpa=input.nextDouble();
    System.out.print("\n \t\tENTER SEMESTER : ");
    i2[i+1].semester=input.nextInt();
    System.out.print("\n \t\tENTER PERCENTAGE :");
    i2[i+1].persentage=input.nextDouble();
    System.out.print("\n \t\tPlease enter Grade : ");
    i2[i+1].Grade=input.next();
    i++;
    }
    public void view_result()
    {
    String n1;
    int reg1;
    int a,j;
    int t=0;
    System.out.println("\n \t\t: VEIW RESULT :");
    System.out.println("\n \t\t 1:NAME");
    System.out.println("\n \t\t 2.REG #");
    Scanner input=new Scanner(System.in);
    a=input.nextInt();
    switch(a){
    case 1:
    t=0;
    System.out.print("\n \t\tENTER NAME : ");
    n1=input.next();
    for(j=0 ; j<=i ; j++)
    {
    a=0;
    if(i2[j].name.equals(n1))
    {
    t++;
    a=j;
    System.out.print("\n \t\t NAME : "+i2[a].name);
    System.out.print("\n \t\t GRADE : "+i2[a].Grade);
    System.out.print("\n \t\t CGPA : "+i2[a].cgpa);
    }
    else if(t==0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    }
    break;
    case 2:
    t=0;
    System.out.print("ENTER REG #");
    reg1=input.nextInt();
    for(j=0 ;j<=i ;j++)
    {
    a=0;
    if(i2[j].reg_no==(reg1))
    {
    t++;
    a=i;
    System.out.print("\n \t\t NAME : "+i2[a].name);
    System.out.print("\n \t\t GRADE : "+i2[a].Grade);
    System.out.print("\n \t\t CGPA : "+i2[a].cgpa);
    }
    else if(t==0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    }
    break;
    default :
    System.out.println("\n \t\twrong input");
    }
    }
    public void edit_result()
    {
    String e_name,grade;
    int r,sir,choice=0;
    double gpa;
    int t=0;
    Scanner input=new Scanner(System.in);
    System.out.println("\n \t\t: EDITRESULT :");
    System.out.println("\n \t\t1.BY NAME");
    System.out.println("\n \t\t2.BY REG #");
    System.out.print("\n \t\tOPTION:");
    sir=input.nextInt();
    switch(sir)
    {
    case 1:
    t=0;
    int a=0;
    System.out.print("\n \t\tENTER NAME : ");
    e_name=input.next();
    for(int j=0 ;j<=i ;j++)
    {
    a=0;
    if(i2[j].name.equals(e_name))
    {
    t++;
    a=i;
    System.out.print("\n \t\t ENTER NEW GRADE : ");
    i2[a].Grade =input.next();
    System.out.print("\n \t\t ENTER NEW CGPA");
    i2[a].cgpa =input.nextDouble();
    }
    else if(t==0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    }
    break;
    case 2:
    t=0;
    System.out.print("\n \t\t ENTER REG #");
    e_name=input.next();
    for(int j=0 ;j<=i ;j++)
    {
    a=0;
    if(i2[j].name.equals(e_name))
    {
    t++;
    a=j;
    System.out.print("\n \t\t ENTER NEW GRADE : ");
    i2[a].Grade =input.next();
    System.out.print("\n \t\t ENTER NEW CGPA");
    i2[a].cgpa =input.nextDouble();
    }
    else if(t==0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    }
    break;
    }
    }
    public void writing()
    {
    if(i>=0)
    {
    try
    {
    ObjectOutputStream out=new ObjectOutputStream( new FileOutputStream("islamic.ser"));
    for (int j=0 ; j<=i ; j++)
    {
    System.out.println("working");
    out.writeObject(i2[j]);
    }
    out.close();
    // writefile.close();
    }
    catch(IOException e)
    {
    System.err.println("File could not be written into");
    }
    }
    try
    {
    Formatter output=new Formatter("count.txt");

    output.format("%10d",i);
    output.close();
    }
    catch(IOException e)
    {
    System.err.println("Text file could not be written into");
    }
    }
    public void reading()
    {
    try
    {
    boolean ans=true;
    Scanner counter=new Scanner(new File("count.txt"));
    i=counter.nextInt();
    if(i>=0)
    {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("islamic.ser"));
    for(int j=0 ; j<=i ;j++)
    {
    i2[j]=new Admin();
    i2[j]=(Admin) in.readObject();

    }
    in.close();

    }
    }
    catch(IOException e)
    {
    System.err.println("File could not be read from");
    }
    catch ( ClassNotFoundException classNotFoundException )
    {
    System.err.println( "Unable to create object." );
    }
    }
    public void menu()
    {
    int option;
    do
    {
    Scanner input=new Scanner(System.in);
    int m,ans,choice,count=0,choice1;
    System.out.println(" \n \t\tADMINISTRATION ");
    System.out.println("\n \t\t1:ENTER PROFILE");
    System.out.println("\n \t\t2:VEIW PROFILE");
    System.out.println("\n \t\t3:EDIT PROFILE");
    System.out.println("\n \t\t4:VEIW RESULT");
    System.out.println("\n \t\t5:ENTER RESULT");
    System.out.println("\n \t\t6:EDIT RESULT");
    System.out.println("\n \t\t7:EXIT");
    System.out.print("\n \t\tOPTION : ");
    m=input.nextInt();
    switch(m)
    {
    case 1:
    enter_profile();
    break;
    case 2:
    if(i<0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    else
    {
    view_profile();
    }
    break;
    case 3:
    if(i<0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    else
    {
    edit_profile();
    }
    break;
    case 4:
    if(i<0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    else
    {
    view_result();
    }
    break;
    case 5:
    enter_result();
    break;
    case 6:
    if(i<0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    else
    {
    edit_result();
    }
    break;
    case 7:
    return;
    }
    System.out.println("\n \t\tDO YOU WANT TO ENTER MORE");
    option=input.nextInt();
    }while(option==1);
    }
    }









    STUDENT CLASS
    ///////////////////////////////////////////////////////////////////////////

    import java.util.Scanner;
    public class Student extends iiui
    implements std
    {
    Student()
    {
    super();
    }
    public boolean login(String u_name,String password)
    {
    int ans;
    if(password.equals("123")&&u_name.equals("student" ))
    return true;
    else
    return false;
    }
    public void profile(iiui i1)
    {
    String n1;
    int reg1;
    int a,j;
    System.out.println("\n \t\t ENTER NAME : 1");
    System.out.println("\n \t\t ENTER REG # : 2");
    Scanner input=new Scanner(System.in);
    a=input.nextInt();
    switch(a){
    case 1:
    System.out.println("\n \t\t ENTER NAME");
    n1=input.next();
    for(j=0 ; j<=i ; j++)
    {
    if(i1.i2[j].name.equals(n1))
    {
    System.out.print("\n \t\t NAME : "+i1.i2[j].name);
    System.out.print("\n \t\t REG # : "+i1.i2[j].reg_no);
    System.out.print("\n \t\t FATHER NAME : "+i1.i2[j].f_name);
    System.out.print("\n \t\t AGE : "+i1.i2[j].age);
    System.out.print("\n \t\t NATIONALITY : "+i1.i2[j].nationality);
    System.out.print("\n \t\t CGPA : "+i1.i2[j].cgpa);
    System.out.print("\n \t\t GRADE : "+i1.i2[j].Grade);
    System.out.print("\n \t\t PERCENTAGE : "+i1.i2[j].persentage);
    }
    else
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    }
    break;
    case 2:
    System.out.println("\n \t\t ENTER REG # : ");
    reg1=input.nextInt();
    for(j=0 ;j<=i ;j++)
    {
    if(i1.i2[j].reg_no==(reg1))
    {
    System.out.print("\n \t\t NAME : "+i1.i2[j].name);
    System.out.print("\n \t\t GRADE : "+i1.i2[j].Grade);
    System.out.print("\n \t\t CGPA : "+i1.i2[j].cgpa);
    }
    else
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    }
    break;
    default :
    System.out.println("\n \t\twrong input");
    }

    }
    public void result(iiui i1)
    {
    String n1;
    int reg1;
    int a,j;
    System.out.print("\n \t\tENTER NAME : 1");
    System.out.print("\n \t\t ENTER REG # : 2");
    System.out.print("\n \t\tOPTION:");
    Scanner input=new Scanner(System.in);
    a=input.nextInt();
    switch(a){
    case 1:
    System.out.print("\n \t\t ENTER NAME : ");
    n1=input.next();
    for(j=0 ; j<=i ; j++)
    {
    if(i1.i2[j].name.equals(n1))
    {

    System.out.print("\n \t\t NAME : "+i1.i2[j].name);
    System.out.print("\n \t\t GRADE : "+i1.i2[j].Grade);
    System.out.print("\n \t\t CGPA : "+i1.i2[j].cgpa);
    }
    else
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    }
    break;
    case 2:
    System.out.println("\n \t\tENTER REG # : ");
    reg1=input.nextInt();
    for(j=0 ;j<=i ;j++)
    {
    if(i1.i2[j].reg_no==(reg1))
    {
    System.out.print("\n \t\t NAME : "+i1.i2[j].name);
    System.out.print("\n \t\t GRADE : "+i1.i2[j].Grade);
    System.out.print("\n \t\t CGPA : "+i1.i2[j].cgpa);
    }
    else
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    }
    break;
    default :
    System.out.println("\n \t\twrong input");
    }
    }
    public void reading()
    {
    }
    public void writing()
    {
    }

    public void menu2(iiui i1)
    {
    Scanner input=new Scanner(System.in);
    int m,ans,choice,count=0,choice1;
    System.out.println("\n \t\tSTUDENT SECTION");
    System.out.println("\n \t\t VEIW PROFILE : 1");
    System.out.println("\n \t\tVEIW RESULT : 2");
    System.out.print("\n \t\tOPTION:");
    m=input.nextInt();
    switch(m)
    {
    case 1:
    if(i<0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    else
    {
    profile(i1);
    }
    break;
    case 2:
    if(i<0)
    {
    System.out.println("\n \t\t NO DATA PRESENT ");
    }
    else
    {
    result(i1);
    }
    break;
    default :
    System.out.println("wrong input");
    }
    }

    }

    MAIN CLASS
    ////////////////////////////////////////////////////////////
    import java.util.Scanner;
    public class Main {


    public static void main(String[] args)
    {
    iiui ob1=new Admin();
    ob1.reading();
    iiui ob=new Student();
    int i=0,j=0,option;
    boolean ans1;
    boolean ans3=false;
    String a;
    do
    {
    Scanner input=new Scanner(System.in);
    System.out.println(" \t*************************************");
    System.out.println("\n \t* ISLAMIC INTERNATIONAL UNIVERSITY * \t\n ");
    System.out.println(" \t*************************************");
    System.out.println("\n \t\t1: LOG IN");
    System.out.println("\n \t\t2: EXIT");
    System.out.print("\n \t\tOPTION :");
    j=input.nextInt();
    switch(j)
    {
    case 1:
    System.out.println("****************************** *************************************");
    System.out.println("\n\n\t\tADMINISTRATION : Admin\n\t\t STUDENT : student");
    System.out.print("\n\t\tENTER USERNAME :");

    String u_name=input.next();
    System.out.print("\n\t\tENTER PASSWORD");
    String password=input.next();
    ans1=ob1.login(u_name, password);
    if(ans1==true)
    {
    System.out.println("\n \t\tWELCOME TO ADMINISTRATION");
    }
    else
    {
    ans3=ob.login(u_name,password);
    if(ans3==true)
    {
    System.out.println("\n \t\tWELCOME TO STUDENT SECTION");
    }
    else
    {
    System.out.println("\n \t\t\tERROR");
    System.out.println("\n \t\tInvalid username and password");
    }
    }
    if((ans1==true)||(ans3==true))
    {

    System.out.println("\n \t\t1: FBAS");
    System.out.println("\n \t\t2: EXIT");
    System.out.print("\n \t\tOPTION:");
    i=input.nextInt();
    if(i==1)
    {
    System.out.println("\n \t\ta. For DCSSE");
    System.out.println("\n \t\tb. For EXIT");
    System.out.print("\n \t\tOPTION:");
    a=input.next();
    switch(a.charAt(0))
    {

    case 'a':
    if(ans1==true)
    {
    ob1.menu();
    break;
    }
    else
    {
    ob.menu2(ob1);
    break;
    }

    case 'b':
    break;
    }

    }
    }
    break;
    case 2:
    ob1.writing();
    // ob.writing();
    // ob1.reading();
    System.exit(1);
    }
    System.out.println(" Do You Want To Continue ");
    option=input.nextInt();
    ob1.writing();
    //ob.writing();
    }while(option==1);
    }
    }
    ////////////////////////////////////////////////////
    IIUI CLASS
    abstract public class iiui implements adm,std
    {
    String name,f_name;
    int reg_no,age,semester;
    double cgpa,persentage;
    String Grade, nationality;
    iiui i2[];
    static int i=-1;
    iiui()
    {
    name=" ";
    f_name=" ";
    reg_no=0;
    age=0;
    cgpa=0.0;
    Grade=" ";

    }
    abstract public boolean login(String u_name , String password);
    abstract public void reading();
    abstract public void writing();
    public void menu()
    {
    }
    public void menu2(iiui i1)
    {

    }
    public void enter_profile()
    {
    }
    public void view_profile()
    {
    }
    public void edit_profile()
    {
    }
    public void enter_result()
    {
    }
    public void view_result()
    {
    }
    public void edit_result()
    {
    }
    public void result()
    {
    }
    public void profile()
    {
    }
    }
    //////////////////////////////////
    ADMIN INTERFACE
    /////////////////////////////////////

    public interface adm
    {
    public boolean login(String u_name,String password);
    public void enter_profile();
    public void view_profile();
    public void edit_profile();
    public void enter_result();
    public void view_result();
    public void edit_result();
    public void menu();

    }
    ////////////////////////////////
    student interface
    /////////////////////////////

    public interface std
    {
    public boolean login(String u_name,String password);
    public void result();
    public void profile();
    public void menu2(iiui i1);
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: NULL pointer exception in main ...PLEASE try solving it

    Quote Originally Posted by aryaashraff View Post
    ADMIN CLASS
    /////////////////////////////////////////////////////////////////////////////////////

    import java.util.Scanner;
    import java.util.Scanner;
    import java.util.Formatter;
    import java.io.*;
    public class Admin extends iiui
            implements adm
    {
        Admin()
        {
            super();
            i2=new Admin[100];
     
     
        }
      public boolean login(String u_name,String password)
        {
                int ans;
                    if(password.equals("12345")&&u_name.equals("admin"))
                        return true;
     
                    else
                       return false;
        }
        public void enter_profile()
        {
             i2[i+1]= new Admin();
             Scanner input=new Scanner(System.in);
             System.out.print("\n \n\t\t ENTER NAME : ");
             i2[i+1].name=input.next();
     
            System.out.print("\n \n\t\tENTER REG # : ");
             i2[i+1].reg_no=input.nextInt();
     
             System.out.print("\n \n\t\tENTER FATHER NAME : ");
             i2[i+1].f_name=input.next();
             System.out.println("\n \t\tENTER SEMESTER : ");
             i2[i+1].semester =input.nextInt();
             System.out.print("\n \n\t\tENTER AGE : ");
             i2[i+1].age=input.nextInt();
             System.out.print("\n \n\t\tENTER NATIONALITY : ");
             i2[i+1].nationality=input.next();
             System.out.print("\n \n\t\tENTER CGPA : ");
             i2[i+1].cgpa=input.nextDouble();
             System.out.print("\n \n\t\tENTER GRADE : ");
             i2[i+1].Grade=input.next();
             System.out.print("\n \n\t\tENTER PERCENTAGE : ");
             i2[i+1].persentage=input.nextDouble();
             i++;
        }
        public void view_profile()
        {
            String name;
            int reg;
            int a,count;
            int t=0;
     
            System.out.println("\n \t\t1:View By Name");
            System.out.println("\n \t\t2:View By Registration number");
            System.out.print("\n \t\tOPTION : ");
            Scanner input=new Scanner(System.in);
            a=input.nextInt();
            switch(a){
                case 1:
                      t=0;
                    System.out.print("\n \n\t\tENTER NAME : ");
                    name=input.next();
                    for(count=0 ; count<=i ; count++)
                    {
                         a=0;
                         if(i2[count].name.equals(name))
                        {
                            t++;
                            a=count;
                            System.out.print("\n \t\tRegistration number :"+i2[a].reg_no);
                            System.out.print("\n \t\tName :               "+i2[a].name);
                            System.out.print("\n \t\tFather name :        "+i2[a].f_name);
                            System.out.print("\n \t\tSemester :           "+i2[a].semester);
                            System.out.print("\n \t\tAge :                "+i2[a].age);
                            System.out.print("\n \t\tGrade :              "+i2[a].Grade);
                            System.out.print("\n \t\tcgpa :               "+i2[a].cgpa);
                            System.out.print("\n \t\tPercentage :         "+i2[a].persentage);
                        }
                    }
                   if(t==0)
                   {
                   System.out.println("\n \t\t NO DATA PRESENT  ");
                   }
     
                    break;
                case 2:
                    t=0;
                    System.out.print("\n \t\tENTER REG #");
                    reg=input.nextInt();
                    for(count=0 ;count<=i ;count++)
                    {
                        a=0;
                      if(i2[count].reg_no==reg)
                        {
                          t++;
                            a=count;
                            System.out.print("\n \t\tRegistration number :"+i2[a].reg_no);
                            System.out.print("\n \t\tName :               "+i2[a].name);
                            System.out.print("\n \t\tFather name :        "+i2[a].f_name);
                            System.out.print("\n \t\tSemester :           "+i2[a].semester);
                            System.out.print("\n \t\tAge :                "+i2[a].age);
                            System.out.print("\n \t\tGrade :              "+i2[a].Grade);
                            System.out.print("\n \t\tcgpa :               "+i2[a].cgpa);
                            System.out.print("\n \t\tPercentage :         "+i2[a].persentage);
     
                        }
                    }
                     if(t==0)
                   {
                   System.out.println("\n \t\t NO DATA PRESENT  ");
                   }
                    break;
                default :
                    System.out.println("\n \t\tWrong input");
            }
     
        }
        public void edit_profile()
        {
            int t=0;
            String name,name2,grade;
            int regno,age1,semester1,choice=0,ans;
            double gpa,persentage1;
            Scanner input=new Scanner(System.in);
     
            System.out.println("\n \t\t1:Edit by Name");
            System.out.println("\n \t\t2:Edit by Registration number");
                System.out.print("\n \t\tOPTION:");
            ans=input.nextInt();
            switch(ans)
            {
                case 1:
                     t=0;
                        System.out.print("\n \t\tENTER NAME:  ");
                        name=input.next();
                        for(int j=0 ;j<=i ;j++)
                        {
                           int a=0;
                            if(i2[j].name.equals(name))
                            {
                                 t++;
                                a=j;
                                System.out.print("\n \t\tEnter New Registration number: ");
                                i2[a].reg_no=input.nextInt();
                                System.out.print("\n \t\tEnter New Name : ");
                                i2[a].name =input.next();
                                System.out.print("\n \t\tEnter New Father name : ");
                                i2[a].f_name =input.next();
                                System.out.print("\n \t\tEnter New Age: ");
                                i2[a].age =input.nextInt();
                                System.out.print("\n \t\tEnter New Semester : ");
                                i2[a].semester =input.nextInt();
                                System.out.print("\n \t\tEnter New Grade : ");
                                i2[a].Grade =input.next();
                                System.out.print("\n \t\tEnter New cgpa: ");
                                i2[a].cgpa =input.nextDouble();
                                System.out.print("\n \t\tEnter New Percentage : ");
                                i2[a].persentage =input.nextDouble();
                            }
                        }
                            if(t==0)
                           {
                             System.out.println("\n \t\t NO DATA PRESENT  ");
                            }
                      break;
                case 2:
                    t=0;
                         System.out.println("\n \t\tENTER REG # :  ");
                         regno=input.nextInt();
                            for(int j=0 ;j<=i ;j++)
                            {
                                if(i2[j].reg_no==regno)
                                {
                                    t++;
                                    int a=j;
                                    System.out.println("\n \t\tEnter New Registration number");
                                i2[a].reg_no=input.nextInt();
                                System.out.println("\n \t\tEnter New Name");
                                i2[a].name =input.next();
                                System.out.println("\n \t\tEnter New Father name");
                                i2[a].f_name =input.next();
                                System.out.println("\n \t\tEnter New Age");
                                i2[a].age =input.nextInt();
                                System.out.println("\n \t\tEnter New Semester");
                                i2[a].semester =input.nextInt();
                                System.out.println("\n \t\tEnter New Grade");
                                i2[a].Grade =input.next();
                                System.out.println("\n \t\tEnter New cgpa");
                                i2[a].cgpa =input.nextDouble();
                                System.out.println("\n \t\tEnter New Percentage");
                                i2[a].persentage =input.nextDouble();
     
                                }
                            }
                               if(t==0)
                                {
                                 System.out.println("\n \t\t NO DATA PRESENT  ");
                                 }
     
                             break;
            }
        }
        public void enter_result()
        {
            i2[i+1]= new Admin();
            Scanner input=new Scanner(System.in);
            System.out.print("\n \t\tENTER NAME : ");
            i2[i+1].name=input.next();
            System.out.print("\n \t\tENTER REG # : ");
            i2[i+1].reg_no=input.nextInt();
            System.out.print("\n \t\tENTER CGPA : ");
             i2[i+1].cgpa=input.nextDouble();
             System.out.print("\n \t\tENTER SEMESTER : ");
            i2[i+1].semester=input.nextInt();
            System.out.print("\n \t\tENTER PERCENTAGE :");
            i2[i+1].persentage=input.nextDouble();
            System.out.print("\n \t\tPlease enter Grade : ");
            i2[i+1].Grade=input.next();
            i++;
        }
       public void view_result()
        {
            String n1;
            int reg1;
            int a,j;
            int t=0;
            System.out.println("\n \t\t: VEIW RESULT :");
            System.out.println("\n \t\t 1:NAME");
            System.out.println("\n \t\t 2.REG #");
            Scanner input=new Scanner(System.in);
            a=input.nextInt();
            switch(a){
                case 1:
                    t=0;
                    System.out.print("\n \t\tENTER NAME : ");
                    n1=input.next();
                    for(j=0 ; j<=i ; j++)
                    {
                        a=0;
                        if(i2[j].name.equals(n1))
                        {
                            t++;
                            a=j;
                            System.out.print("\n \t\t NAME :  "+i2[a].name);
                            System.out.print("\n \t\t GRADE : "+i2[a].Grade);
                            System.out.print("\n \t\t CGPA :  "+i2[a].cgpa);
                        }
                        else if(t==0)
                        {
                        System.out.println("\n \t\t NO DATA PRESENT  ");
                        }
                    }
                    break;
                case 2:
                    t=0;
                    System.out.print("ENTER REG #");
                    reg1=input.nextInt();
                    for(j=0 ;j<=i ;j++)
                    {
                        a=0;
                      if(i2[j].reg_no==(reg1))
                        {
                          t++;
                          a=i;
                            System.out.print("\n \t\t NAME :  "+i2[a].name);
                            System.out.print("\n \t\t GRADE : "+i2[a].Grade);
                            System.out.print("\n \t\t CGPA :  "+i2[a].cgpa);
                        }
                        else if(t==0)
                        {
                        System.out.println("\n \t\t NO DATA PRESENT  ");
                        }
                    }
                    break;
                default :
                    System.out.println("\n \t\twrong input");
            }
        }
        public void edit_result()
        {
            String e_name,grade;
            int r,sir,choice=0;
            double gpa;
            int t=0;
            Scanner input=new Scanner(System.in);
            System.out.println("\n \t\t: EDITRESULT :");
            System.out.println("\n \t\t1.BY NAME");
            System.out.println("\n \t\t2.BY REG #");
            System.out.print("\n \t\tOPTION:");
            sir=input.nextInt();
            switch(sir)
            {
                case 1:
                    t=0;
                    int a=0;
                     System.out.print("\n \t\tENTER NAME : ");
                     e_name=input.next();
                        for(int j=0 ;j<=i ;j++)
                        {
                            a=0;
                        if(i2[j].name.equals(e_name))
                            {
                            t++;
                             a=i;
                              System.out.print("\n \t\t ENTER NEW GRADE : ");
                            i2[a].Grade =input.next();
                              System.out.print("\n \t\t ENTER NEW CGPA");
                             i2[a].cgpa =input.nextDouble();
                            }
                            else if(t==0)
                            {
                            System.out.println("\n \t\t NO DATA PRESENT  ");
                            }
                        }
                      break;
                case 2:
                    t=0;
                         System.out.print("\n \t\t ENTER REG #");
                         e_name=input.next();
                            for(int j=0 ;j<=i ;j++)
                            {
                                a=0;
                                if(i2[j].name.equals(e_name))
                                {
                                    t++;
                                a=j;
                                 System.out.print("\n \t\t ENTER NEW GRADE : ");
                            i2[a].Grade =input.next();
                              System.out.print("\n \t\t ENTER NEW CGPA");
                             i2[a].cgpa =input.nextDouble();
                                }
                                else if(t==0)
                                {
                                    System.out.println("\n \t\t NO DATA PRESENT  ");
                                }
                            }
                             break;
            }
        }
        public void writing()
       {
           if(i>=0)
           {
            try
            {
                ObjectOutputStream out=new ObjectOutputStream( new FileOutputStream("islamic.ser"));
                for (int j=0 ; j<=i ; j++)
                {
                    System.out.println("working");
                    out.writeObject(i2[j]);
                }
                out.close();
             // writefile.close();
            }
            catch(IOException e)
            {
                   System.err.println("File could not be written into");
            }
          }
           try
           {
            Formatter output=new Formatter("count.txt");
     
            output.format("%10d",i);
            output.close();
           }
           catch(IOException e)
           {
               System.err.println("Text file could not be written into");
           }
       }
      public void reading()
       {
           try
           {
               boolean ans=true;
               Scanner counter=new Scanner(new File("count.txt"));
               i=counter.nextInt();
               if(i>=0)
               {
                ObjectInputStream in = new ObjectInputStream(new FileInputStream("islamic.ser"));
                for(int j=0 ; j<=i ;j++)
                {
                    i2[j]=new Admin();
                    i2[j]=(Admin) in.readObject();
     
                }
               in.close();
     
               }
           }
           catch(IOException e)
           {
              System.err.println("File could not be read from");
           }
           catch ( ClassNotFoundException classNotFoundException )
          {
             System.err.println( "Unable to create object." );
          }
       }
       public void menu()
        {
            int option;
           do
           {
                Scanner input=new Scanner(System.in);
                int m,ans,choice,count=0,choice1;
                System.out.println(" \n \t\tADMINISTRATION          ");
                System.out.println("\n \t\t1:ENTER PROFILE");
                System.out.println("\n \t\t2:VEIW PROFILE");
                System.out.println("\n \t\t3:EDIT PROFILE");
                System.out.println("\n \t\t4:VEIW RESULT");
                System.out.println("\n \t\t5:ENTER RESULT");
                System.out.println("\n \t\t6:EDIT RESULT");
                System.out.println("\n \t\t7:EXIT");
                System.out.print("\n \t\tOPTION : ");
                m=input.nextInt();
               switch(m)
               {
                   case 1:
                       enter_profile();
                       break;
                   case 2:
                       if(i<0)
                       {
                           System.out.println("\n \t\t NO DATA PRESENT ");
                       }
                       else
                       {
                          view_profile();
                       }
                       break;
                   case 3:
                       if(i<0)
                       {
                                System.out.println("\n \t\t NO DATA PRESENT  ");
                       }
                       else
                       {
                          edit_profile();
                       }
                       break;
                   case 4:
                       if(i<0)
                       {
                           System.out.println("\n \t\t NO DATA PRESENT  ");
                       }
                       else
                       {
                          view_result();
                       }
                       break;
                   case 5:
                       enter_result();
                       break;
                   case 6:
                       if(i<0)
                       {
                         System.out.println("\n \t\t NO DATA PRESENT  ");
                       }
                       else
                       {
                          edit_result();
                       }
                        break;
                   case 7:
                       return;
               }
                System.out.println("\n \t\tDO YOU WANT TO ENTER MORE");
                option=input.nextInt();
       }while(option==1);
       }
    }







    STUDENT CLASS
    ///////////////////////////////////////////////////////////////////////////
    import java.util.Scanner;
    public class Student extends iiui
                   implements std
     {
        Student()
        {
            super();
        }
        public boolean login(String u_name,String password)
        {
                int ans;
                if(password.equals("123")&&u_name.equals("student"))
                    return true;
                else
                 return false;
        }
         public void profile(iiui i1)
         {
             String n1;
            int reg1;
            int a,j;
            System.out.println("\n \t\t ENTER NAME : 1");
            System.out.println("\n \t\t ENTER REG # : 2");
            Scanner input=new Scanner(System.in);
            a=input.nextInt();
            switch(a){
                case 1:
                    System.out.println("\n \t\t ENTER NAME");
                    n1=input.next();
                    for(j=0 ; j<=i ; j++)
                    {
                        if(i1.i2[j].name.equals(n1))
                        {
                            System.out.print("\n \t\t NAME : "+i1.i2[j].name);
                            System.out.print("\n \t\t REG # : "+i1.i2[j].reg_no);
                            System.out.print("\n \t\t FATHER NAME : "+i1.i2[j].f_name);
                            System.out.print("\n \t\t AGE : "+i1.i2[j].age);
                            System.out.print("\n \t\t NATIONALITY : "+i1.i2[j].nationality);
                            System.out.print("\n \t\t CGPA : "+i1.i2[j].cgpa);
                            System.out.print("\n \t\t GRADE : "+i1.i2[j].Grade);
                            System.out.print("\n \t\t PERCENTAGE : "+i1.i2[j].persentage);
                       }
                        else
                        {
                        System.out.println("\n \t\t NO DATA PRESENT  ");
                        }
                    }
                    break;
                case 2:
                    System.out.println("\n \t\t ENTER REG # : ");
                    reg1=input.nextInt();
                    for(j=0 ;j<=i ;j++)
                    {
                      if(i1.i2[j].reg_no==(reg1))
                        {
                            System.out.print("\n \t\t NAME : "+i1.i2[j].name);
                            System.out.print("\n \t\t GRADE : "+i1.i2[j].Grade);
                            System.out.print("\n \t\t CGPA : "+i1.i2[j].cgpa);
                        }
                        else
                        {
                        System.out.println("\n \t\t NO DATA PRESENT  ");
                        }
                    }
                    break;
                default :
                    System.out.println("\n \t\twrong input");
            }
     
         }
        public void result(iiui i1)
        {
            String n1;
            int reg1;
            int a,j;
            System.out.print("\n \t\tENTER NAME : 1");
            System.out.print("\n \t\t ENTER REG # : 2");
            System.out.print("\n \t\tOPTION:");
            Scanner input=new Scanner(System.in);
            a=input.nextInt();
            switch(a){
                case 1:
                    System.out.print("\n \t\t ENTER NAME : ");
                    n1=input.next();
                    for(j=0 ; j<=i ; j++)
                    {
                        if(i1.i2[j].name.equals(n1))
                        {
     
                            System.out.print("\n \t\t NAME : "+i1.i2[j].name);
                            System.out.print("\n \t\t GRADE : "+i1.i2[j].Grade);
                            System.out.print("\n \t\t CGPA : "+i1.i2[j].cgpa);
                        }
                        else
                        {
                        System.out.println("\n \t\t NO DATA PRESENT  ");
                        }
                    }
                    break;
                case 2:
                    System.out.println("\n \t\tENTER REG # : ");
                    reg1=input.nextInt();
                    for(j=0 ;j<=i ;j++)
                    {
                      if(i1.i2[j].reg_no==(reg1))
                        {
                           System.out.print("\n \t\t NAME : "+i1.i2[j].name);
                            System.out.print("\n \t\t GRADE : "+i1.i2[j].Grade);
                            System.out.print("\n \t\t CGPA : "+i1.i2[j].cgpa);
                        }
                        else
                        {
                        System.out.println("\n \t\t NO DATA PRESENT  ");
                        }
                    }
                    break;
                default :
                    System.out.println("\n \t\twrong input");
            }
        }
       public void reading()
        {
        }
        public void writing()
        {
        }
     
       public void menu2(iiui i1)
       {
                Scanner input=new Scanner(System.in);
                int m,ans,choice,count=0,choice1;
                System.out.println("\n \t\tSTUDENT SECTION");
                System.out.println("\n \t\t VEIW PROFILE : 1");
                System.out.println("\n \t\tVEIW RESULT : 2");
                System.out.print("\n \t\tOPTION:");
                m=input.nextInt();
               switch(m)
               {
                   case 1:
                       if(i<0)
                       {
                           System.out.println("\n \t\t NO DATA PRESENT  ");
                       }
                       else
                       {
                            profile(i1);
                       }
                       break;
                   case 2:
                       if(i<0)
                       {
                           System.out.println("\n \t\t NO DATA PRESENT  ");
                       }
                       else
                       {
                             result(i1);
                       }
                       break;
                        default :
                    System.out.println("wrong input");
               }
       }
     
    }
    MAIN CLASS
    ////////////////////////////////////////////////////////////
    import java.util.Scanner;
    public class Main {
     
     
        public static void main(String[] args)
        {
            iiui ob1=new Admin();
            ob1.reading();
            iiui ob=new Student();
            int i=0,j=0,option;
            boolean ans1;
            boolean ans3=false;
            String a;
            do
            {
            Scanner input=new Scanner(System.in);
            System.out.println(" \t*************************************");
            System.out.println("\n \t* ISLAMIC INTERNATIONAL UNIVERSITY * \t\n ");
            System.out.println(" \t*************************************");
            System.out.println("\n \t\t1: LOG IN");
            System.out.println("\n \t\t2: EXIT");
             System.out.print("\n \t\tOPTION :");
            j=input.nextInt();
            switch(j)
            {
                case 1:
                        System.out.println("*******************************************************************");
                        System.out.println("\n\n\t\tADMINISTRATION : Admin\n\t\t STUDENT : student");
                        System.out.print("\n\t\tENTER USERNAME :");
     
                        String u_name=input.next();
                        System.out.print("\n\t\tENTER PASSWORD");
                        String password=input.next();
                        ans1=ob1.login(u_name, password);
                        if(ans1==true)
                        {
                            System.out.println("\n \t\tWELCOME TO ADMINISTRATION");
                        }
                        else
                        {
                            ans3=ob.login(u_name,password);
                            if(ans3==true)
                            {
                                System.out.println("\n \t\tWELCOME TO STUDENT SECTION");
                            }
                            else
                            {
                                System.out.println("\n \t\t\tERROR");
                                System.out.println("\n \t\tInvalid username and password");
                            }
                        }
                        if((ans1==true)||(ans3==true))
                        {
     
                        System.out.println("\n \t\t1:  FBAS");
                        System.out.println("\n \t\t2:  EXIT");
                        System.out.print("\n \t\tOPTION:");
                        i=input.nextInt();
                        if(i==1)
                        {
                                System.out.println("\n \t\ta. For DCSSE");
                                System.out.println("\n \t\tb. For EXIT");
                                System.out.print("\n \t\tOPTION:");
                                a=input.next();
                                switch(a.charAt(0))
                                 {
     
                                       case 'a':
                                       if(ans1==true)
                                        {
                                            ob1.menu();
                                            break;
                                        }
                                         else
                                        {
                                            ob.menu2(ob1);
                                            break;
                                        }
     
                                        case 'b':
                                        break;
                                 }
     
                        }
                   }
                   break;
                case 2:
                    ob1.writing();
                  //  ob.writing();
                   // ob1.reading();
                    System.exit(1);
            }
            System.out.println("     Do You Want To Continue    ");
            option=input.nextInt();
            ob1.writing();
            //ob.writing();
            }while(option==1);
        }
    }
    ////////////////////////////////////////////////////
    IIUI CLASS
    abstract public class iiui implements adm,std
    {
    	String name,f_name;
    	int reg_no,age,semester;
    	double cgpa,persentage;
    	String Grade, nationality;
            iiui i2[];
            static int i=-1;
    	iiui()
    	{
    		name=" ";
                    f_name=" ";
    		reg_no=0;
                    age=0;
    		cgpa=0.0;
    		Grade=" ";
     
    	}
            abstract public boolean login(String u_name , String password);
            abstract public void reading();
            abstract public void writing();
            public void menu()
            {
            }
            public void menu2(iiui i1)
            {
     
            }
            public void enter_profile()
            {
            }
            public void view_profile()
            {
            }
            public void edit_profile()
            {
            }
            public void enter_result()
            {
            }
            public void view_result()
            {
            }
            public void edit_result()
            {
            }
            public void result()
            {
            }
            public void profile()
            {
            }
    }
    //////////////////////////////////
    ADMIN INTERFACE
    /////////////////////////////////////
    public interface adm
    {
      public  boolean login(String u_name,String password);
      public void enter_profile();
      public void view_profile();
      public void edit_profile();
      public void enter_result();
      public void view_result();
      public void edit_result();
      public void menu();
     
    }
    ////////////////////////////////
    student interface
    /////////////////////////////
    public interface std
    {
      public boolean login(String u_name,String password);
      public void result();
      public void profile();
      public void menu2(iiui i1);
    }
    Did the error that came with the exception show a location of where in the main is this exception being thrown?

    I have a hard time following your code, but thought I might have noticed something in one of your classes

    for(int j=0 ;j<=i ;j++)

    Last that I checked, the variable i was -1. 0 > -1

    Wait, that would be the editor, I think.

    You should probably use getters and setters instead of the variable names like that.
    Last edited by javapenguin; June 9th, 2012 at 02:26 PM.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    aryaashraff (June 9th, 2012)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,193
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: NULL pointer exception in main ...PLEASE try solving it

    Please post the full text of the error message. It has important information about the error.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  5. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NULL pointer exception in main ...PLEASE try solving it

    ENTER NAME : Exception in thread "main" java.lang.NullPointerException
    a
    at Admin.view_profile(Admin.java:70)
    at Admin.menu(Admin.java:434)
    at Main.main(Main.java:71)
    Java Result: 1

    thts the error , it also throw IOexception tht file couldnt be written

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,193
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: NULL pointer exception in main ...PLEASE try solving it

    Quote Originally Posted by aryaashraff View Post
    ENTER NAME : Exception in thread "main" java.lang.NullPointerException
    a
    at Admin.view_profile(Admin.java:70)
    at Admin.menu(Admin.java:434)
    at Main.main(Main.java:71)
    Java Result: 1

    thts the error , it also throw IOexception tht file couldnt be written
    The code tries to use a variable with a null value at line 70 in Admin. Look at line 70, find the variable with the null value and then backtrack in the code to find out why that variable has a null value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Getting a null pointer exception.
    By Ouzi in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 16th, 2012, 11:25 AM
  2. Help with null pointer exception
    By Dr.HughMan in forum Exceptions
    Replies: 35
    Last Post: November 30th, 2011, 08:00 PM
  3. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  4. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM