Hi,

I am getting a null pointer exception with the code below. From what I have read, it seems like something is null, but I do not understand how this could be a problem? The error is occurring on line 205 of the StudentContainer class. (This is at the very bottom of the code.)

The problem is occurring when I call the parse method of the Student class from the StudentContainer class. Am I setting up the Student objects incorrectly?

Sorry for the large amount of code.

    public class StudentRecords {

        public static void main(String[] args) {
            new StudentContainer();]

    public class Student {
            String firstName = " ";
            String lastName = " ";
            String addressLine1 = " ";
            String addressLine2 = " ";
            String city = " ";
            String state = " ";
            String zip = " ";
            String bdayMonth = " ";
            String bdayDay = " ";
            String bdayYear = " ";
            String gdayMonth = " ";
            String gdayDay = " ";
            String gdayYear = " ";
            String gpa = " ";
            String creditHours = " ";

    //constructor deleted. I have tried declaring variables here, still fails.

    public void parse(String line){

        System.out.println("Student parse method");

        firstName.equals(line.split(","));
        System.out.println();

        lastName.equals(line.split(","));
        System.out.println();

        addressLine1.equals(line.split(","));
        System.out.println();

        addressLine2.equals(line.split(","));
        System.out.println();

        city.equals(line.split(","));
        System.out.println();

        state.equals(line.split(","));
        System.out.println();

        zip.equals(line.split(","));
        System.out.println();

        bdayMonth.equals(line.split(","));
        System.out.println();

        bdayDay.equals(line.split(","));
        System.out.println();

        bdayYear.equals(line.split(","));
        System.out.println();

        gdayMonth.equals(line.split(","));
        System.out.println();

        gdayDay.equals(line.split(","));
        System.out.println();

        gdayYear.equals(line.split(","));
        System.out.println();

        gpa.equals(line.split(","));
        System.out.println();

        creditHours.equals(line.split(","));
        System.out.println();
    }

}


import java.io.*; 
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class StudentContainer extends JFrame implements ActionListener {    
    public int counter = 0;
//  public String[][] asciiPlots = new String[64][FRAMES];                                  //two dimensional array keeps track of changes to TextBoxes

    private static final long serialVersionUID = 1L;
        final int num_students = 50;                        
        Student students[] = new Student[num_students]; 
        String line[] = new String [50]; 

        //  public static final int NUMROWS = 8;
        //  public static final int FRAMES = 10;                                                    //program is limited to 10 frames
        JLabel lblFirstName = new JLabel(" First Name:");                                                   //creates lblName label
        JLabel lblLastName = new JLabel(" Last Name:");                                     //creates lblAddress label
        JLabel lblAddressLine1 = new JLabel(" Address Line 1:");                                        //creates lblAddress label
        JLabel lblAddressLine2 = new JLabel(" Address Line 2:");                                        //creates lblAddress label
        JLabel lblCity = new JLabel(" City:");                                          //creates lblPhone label
        JLabel lblState = new JLabel(" State:");                                        //creates lblAddress label
        JLabel lblZip = new JLabel(" Zip Code:");                                       //creates lblAddress label
        JLabel lblBirthday = new JLabel(" Date of Birth:");                                     //creates lblAddress label
        JLabel lblGradDate = new JLabel(" Graduation Date:");                                       //creates lblAddress label
        JLabel lblGpa = new JLabel(" GPA:");                                        //creates lblAddress label
        JLabel lblCreditHours = new JLabel(" Credit Hours:");                                       //creates lblAddress label

        JButton clickSave = new JButton("Save");                                                //creates Save button
        JButton clickPrevious = new JButton("Previous");                                        //creates Previous button
        JButton clickNext = new JButton("Next");                                                //creates Next button
        JButton clickDelete = new JButton("Delete");                                            //creates Delete button
        JTextField FirstNameInput = new JTextField("");                                             //creates NameInput text field
        JTextField LastNameInput = new JTextField("");                                          //creates AddressInput text field
        JTextField AddressLine1Input = new JTextField("");  
        JTextField AddressLine2Input = new JTextField("");
        JTextField CityInput = new JTextField("");
        JTextField StateInput = new JTextField("");
        JTextField ZipInput = new JTextField("");
        JTextField BirthdayInput = new JTextField("");
        JTextField GradDateInput = new JTextField("");
        JTextField GpaInput = new JTextField("");
        JTextField CreditHoursInput = new JTextField("");
        //creates PhoneInput text field                             //creates 64 text boxes for 8x8 ASCII 

    public StudentContainer() {
        super("STUDENT DATABASE");                                                          //container header is "ASCII PROGRAM"                       
        newGUI();                                                                           //calls newGUI to initialize GUI 
        readFile();                                                                 
    } // end constructor{

    private void newGUI(){
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                               //exit on close statement
         Container p = this.getContentPane();                                               //declare container p
         p.setLayout(new BorderLayout());                                                   //set p to BorderLayout
         Panel pCenter = new Panel();                                                       //creates pCenter panel

         pCenter.setLayout(new GridLayout(0,2));                                            //initiates grid layout
         pCenter.add(lblFirstName);                                                             //adds Name label to pCenter
         pCenter.add(FirstNameInput);                                                           //adds Phone text field to pCenter
         pCenter.add(lblLastName);                                                              //adds Name label to pCenter
         pCenter.add(LastNameInput);                                                            //adds Phone text field to pCenter
         pCenter.add(lblAddressLine1);                                                          //adds Name text field to pCenter   
         pCenter.add(AddressLine1Input);                                                            //adds Phone text field to pCenter
         pCenter.add(lblAddressLine2);                                                              //adds Name label to pCenter
         pCenter.add(AddressLine2Input);                                                            //adds Phone text field to pCenter
         pCenter.add(lblCity);                                                              //adds Name label to pCenter
         pCenter.add(CityInput);                                                            //adds Phone text field to pCenter
         pCenter.add(lblState);                                                             //adds Name label to pCenter
         pCenter.add(StateInput);                                                           //adds Phone text field to pCenter
         pCenter.add(lblZip);                                                               //adds Name label to pCenter
         pCenter.add(ZipInput);                                                         //adds Phone text field to pCenter
         pCenter.add(lblBirthday);                                                              //adds Name label to pCenter
         pCenter.add(BirthdayInput);                                                            //adds Phone text field to pCenter
         pCenter.add(lblGradDate);                                                              //adds Name label to pCenter
         pCenter.add(GradDateInput);                                                            //adds Phone text field to pCenter
         pCenter.add(lblGpa);                                                               //adds Name label to pCenter
         pCenter.add(GpaInput);                                                         //adds Phone text field to pCenter
         pCenter.add(lblCreditHours);                                                               //adds Name label to pCenter
         pCenter.add(CreditHoursInput);                                                         //adds Phone text field to pCenter


         Panel pSouth = new Panel();                                                        //creates pSouth panel
         pSouth.add(clickPrevious);                                                         //adds previous button
         pSouth.add(clickSave);                                                             //adds save button
         pSouth.add(clickDelete);                                                           //adds delete button
         pSouth.add(clickNext);                                                             //adds next button      

         p.add(pCenter, BorderLayout.CENTER);                                               //adds pCenter panel to container
         p.add(pSouth, BorderLayout.SOUTH);                                                     //adds pSouth panel to container

         clickPrevious.addActionListener(this);                                             //assigns ActionListener to previous button
         this.pack();   
         this.setVisible(true); 

         clickSave.addActionListener(this);                                                 //assigns ActionListener to save button
         this.pack();   
         this.setVisible(true); 

         clickDelete.addActionListener(this);                                               //assigns ActionListener to delete button
         this.pack();
         this.setVisible(true);

         clickNext.addActionListener(this);                                                 //assigns ActionListener to next button
         this.pack();

//         ReadFile();                                                                      //calls ReadFile to read the AddressBook.txt file into the ArrayLists
//         if (Name.size() != 0)                                                            //if there is no empty data
//          counter = 0;                                                                    //set the counter to the first record
//       else
//          counter = -1;                                                                   //set the counter to -1 if there is no data in the text file
////         updateScreen();                                                                //calls update screen to show blank text fields
//         this.setVisible(true);           
    }//end newGUI

    public void actionPerformed(ActionEvent e) {
//        if (e.getSource() == clickSave){                                                  //if save button is clicked, save unless there is nothing to save
//              String x = NameInput.getText();
//              String y = AddressInput.getText();
//              String z = PhoneInput.getText();
//              
//              if(x.equals("")){                                                               //if name is blank
//                  JOptionPane.showMessageDialog(null, "Name must be entered!");               //display error message
//              }//end else if
//              else if(y.equals("")){                                                          //if address is blank
//                  JOptionPane.showMessageDialog(null, "Address must be entered!");            //display error message
//              }//end else if      
//              else if(z.equals("")){                                                          //if phone number is blank
//                  JOptionPane.showMessageDialog(null, "Phone Number must be entered!");       //display error message
//              }//end else if
//              else{                                                                           //if all items are entered  
//          Name.add(NameInput.getText());                                                      //updates list with Name
//          Address.add(AddressInput.getText());                                                //updates list with Address
//          Phone.add(PhoneInput.getText());                                                    //updates list with Phone
//          WriteFile();                                                                        //calls WriteFile to save to the text file
//          counter = Name.size() - 1;                                                          //takes counter to last record of list                                                          
//          JOptionPane.showMessageDialog(null, "Save is complete!");                           //display message to confirm save to user
//              }//end else
//              
//          }//end if
//      
//      else if (e.getSource() == clickDelete){                                                 //if delete is clicked, delete unless there is nothing to delete    
//          if(counter >= 0){                                                                   //if the counter is greater than 0
//              Name.remove(counter);                                                           //remove name from name list
//              Address.remove(counter);                                                        //remove address from address list
//              Phone.remove(counter);                                                          //remove phone number from phone list
//              WriteFile();                                                                    //deletes record from text file
//              if (counter == Name.size()){                                                    //if counter was last record
//                  if (Name.size() == 0)                                                       //if size is empty then counter becomes -1
//                      counter = -1;
//                  else
//                      counter = counter - 1;                                                  //sets counter to previous record
//              }               
//              updateScreen();                                                                 //call update method to display blank text fields
//              JOptionPane.showMessageDialog(null, "Entry has been Deleted!");                 //display message to confirm save to user
//          }//end if
//          else {          
//              BlankGUI();                                                                     //if counter is not greater than 0
//              JOptionPane.showMessageDialog(null, "There are no records!");                   //display error message to user
//          }//end else
//      }//end else if
//      
//      else if (e.getSource() == clickPrevious){                                               //if previous is clicked, go to previous unless there is no previous
//          if(counter > 0){                                                                    //if counter is greater than 0
//              counter--;                                                                      
//              updateScreen();                                                                 //call update method to display blank form
//          }           
//          else
//              JOptionPane.showMessageDialog(null, "This is the first record");                //display message to user
//      }//end else if
//      
//      else if (e.getSource() == clickNext){                                                   //if next is clicked, go to next unless there is nothing next.
//          if(counter > -1){                                                                   //if the arrayList is not empty
//                                                                                          
//              if (counter<Name.size() - 1){                                                    //if we are not at the end of the ArrayList, go to next element
//                  counter++;                                                                  //go to next record
//                  updateScreen();
//              }//end if
//              else JOptionPane.showMessageDialog(null, "This is the last record!");           //else display message to let user know we are at the end
//      
//          }//end if       
//          else JOptionPane.showMessageDialog(null, "There are no records!");                  //display message to let user know there are no records
//      
//      }//end else if
//      
//      else JOptionPane.showMessageDialog(null, "Error!");     
    }

    public void readFile(){
        BufferedReader readBuffer;
        try {
            readBuffer = new BufferedReader(new FileReader("Students.txt"));

            String strRead;
            int p = 0;
            while((strRead = readBuffer.readLine())!=null){                             
                System.out.println("readFile first step in while");
                String line1 = strRead; 
                System.out.println(line1);
                System.out.println("readFile first step in while2");
                students[p].parse(line1);       //<==Null Pointer??
                System.out.println("readFile after first parse call");
                System.out.println(line[p]);
            }
        }   
            catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   


}
String strRead;
int p = 0;
while((strRead = readBuffer.readLine())!=null){
System.out.println("readFile first step in while");
String line1 = strRead;

this construct sets line1 to null - could this be your problem?

You forget this

students[0] = new Student();

same goes for all other...

commented: This is correct. Ignore what I said. :) +11

Are you saying strRead is null for the first line of the text file? I don't think that is the case, but I will try to verify that.

you haven't initialized Student class object before using it....

After declaring array , u have to initialized the objects one by one , or through loop.

Are you saying strRead is null for the first line of the text file? I don't think that is the case, but I will try to verify that.

students[p].parse(line1); //<==Null Pointer?? I believe that Majestics is right. You initialize the students array, but the elements of the array are null.

students is not null: Student students[] = new Student[num_students]; But did you initialize the students[p] ?

I mean

tempclass c[] = new tempclass[10];
c[0] = new tempclass();// with or without parameter...

Above is just a example, dont confuse yourself by looking in code.... :-)

Are you saying strRead is null for the first line of the text file? I don't think that is the case, but I will try to verify that.

Sorry, I misread. I saw an imaginary close brace where none existed.

Carry on. :)

(this is why we say please use the code tags - it makes it much easier to read!)

Thank you Majestics!

I added a for loop to initialize the students individually and it works. Is the readFile method a bad place for this loop?

public void readFile(){
    for(int j = 0; j<50; j++){
        students[j] = new Student();
    }

    BufferedReader readBuffer;
    try {
        readBuffer = new BufferedReader(new FileReader("Students.txt"));

        String strRead;
        int p = 0;
        while((strRead = readBuffer.readLine())!=null){                             
            System.out.println("readFile first step in while");
            String line1 = strRead; 
            System.out.println(line1);
            System.out.println(line1);
            System.out.println("readFile first step in while2");
            students[p].parse(line1);
            System.out.println("readFile after first parse call");
            System.out.println(line[p]);
        }
    }   
        catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}   

why dont u make a separate function and use it in constructor to initialize....

i mean

function initloop()
{
write loop here 
}

and write initloop() in constructor of StudentContainer...

Now I am having trouble parsing the line from the text file. Below is an example of one of the lines.

Bill,Jones,123 111th Street,,Indianapolis,IN,46201,7,14,1992,6,13,2016,2,94

I originally wrote this program in c++ and used the getline function. I am having trouble with the java version. Split wants to put the line into an array.

Yes, that's what split does. So as long as each field is represented, either with data or blank, as in your address2 field, you know where it is, and you can assign it to the right place. I'd probably make a constructor for a student object that takes that String, splits it, and assigns the data to the right places.

Got it...

[String delim = ",";
String tokens[] = line.split(delim);

firstName = tokens[0];]

Jon,

I plan to use the parse method to split and assign the string data. You think it would be better to have this in the constructor?

If you're using this data to construct a student, you might as well just pass one parameter instead of a dozen or so. I'd take the String and do something like

public Student(String bigUglyString)
{
  String[] tokens = bigUglyString.split(",");
  this.FirstName=tokens[0];
  this.LastName=tokens[1];
   ...
}

Easier than splitting it and passing each token individually, I think.

Ok, thanks.

Another questions. I am working in the constructor of Student. The first two lines of the code below work, but the second two lines do not.

[ Student students[] = new Student[9];
students[9] = new Student();

Date birthday = new Date;
birthday = new Date();]

I will be deleting the first two lines, I was just using them as an example. What do I need to do to get the Date object to initialize?

Call its constructor. You got it partly right on each line.

Date birthday = new Date();

(by the way, for code tags, you want the word CODE, in brackets: (CODE). Same thing again at the end of the block of code, but with a leading slash: /CODE, in square brackets. There's a little button above the text entry box that'll generate that, just select your code and click "(CODE)")

I was going to ask what you meant by code blocks, thanks again.

Below is the StudentContainer class. On Line 233, it does not recognize birthday. Do I need some kind of pointer? I created the birthday object in the Student class.

import java.io.*; 
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class StudentContainer extends JFrame implements ActionListener {	
    public int counter = 0;
	
	private static final long serialVersionUID = 1L;
		final int num_students = 50;						
		Student students[] = new Student[num_students];	
		String line[] = new String [50];
		
		JLabel lblFirstName = new JLabel(" First Name:");									//creates lblName label
	    JLabel lblLastName = new JLabel(" Last Name:");										//creates lblAddress label
		JLabel lblAddressLine1 = new JLabel(" Address Line 1:");							//creates lblAddress label
	    JLabel lblAddressLine2 = new JLabel(" Address Line 2:");							//creates lblAddress label
	    JLabel lblCity = new JLabel(" City:");												//creates lblPhone label
	    JLabel lblState = new JLabel(" State:");											//creates lblAddress label
	    JLabel lblZip = new JLabel(" Zip Code:");											//creates lblAddress label
	    JLabel lblBirthday = new JLabel(" Date of Birth:");									//creates lblAddress label
	    JLabel lblGradDate = new JLabel(" Graduation Date:");								//creates lblAddress label
	    JLabel lblGpa = new JLabel(" GPA:");												//creates lblAddress label
	    JLabel lblCreditHours = new JLabel(" Credit Hours:");								//creates lblAddress label
	    
	    JButton clickSave = new JButton("Save");											//creates Save button
	    JButton clickPrevious = new JButton("Previous");										//creates Previous button
	    JButton clickNext = new JButton("Next");												//creates Next button
	    JButton clickDelete = new JButton("Delete");											//creates Delete button
	    JTextField FirstNameInput = new JTextField("");												//creates NameInput text field
	    JTextField LastNameInput = new JTextField("");											//creates AddressInput text field
	    JTextField AddressLine1Input = new JTextField("");	
	    JTextField AddressLine2Input = new JTextField("");
	    JTextField CityInput = new JTextField("");
	    JTextField StateInput = new JTextField("");
	    JTextField ZipInput = new JTextField("");
	    JTextField BirthdayInput = new JTextField("");
	    JTextField GradDateInput = new JTextField("");
	    JTextField GpaInput = new JTextField("");
	    JTextField CreditHoursInput = new JTextField("");
	
	public StudentContainer() {
        super("STUDENT DATABASE");															//container header is "ASCII PROGRAM"						
        newGUI();																			//calls newGUI to initialize GUI 
        readFile();
        updateScreen();
	} // end constructor{

	private void newGUI(){
		 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);								//exit on close statement
         Container p = this.getContentPane();												//declare container p
         p.setLayout(new BorderLayout());													//set p to BorderLayout
         Panel pCenter = new Panel();														//creates pCenter panel
         	
         pCenter.setLayout(new GridLayout(0,2));											//initiates grid layout
         pCenter.add(lblFirstName);																//adds Name label to pCenter
         pCenter.add(FirstNameInput);															//adds Phone text field to pCenter
         pCenter.add(lblLastName);																//adds Name label to pCenter
         pCenter.add(LastNameInput);															//adds Phone text field to pCenter
         pCenter.add(lblAddressLine1);															//adds Name text field to pCenter	
         pCenter.add(AddressLine1Input);															//adds Phone text field to pCenter
         pCenter.add(lblAddressLine2);																//adds Name label to pCenter
         pCenter.add(AddressLine2Input);															//adds Phone text field to pCenter
         pCenter.add(lblCity);																//adds Name label to pCenter
         pCenter.add(CityInput);															//adds Phone text field to pCenter
         pCenter.add(lblState);																//adds Name label to pCenter
         pCenter.add(StateInput);															//adds Phone text field to pCenter
         pCenter.add(lblZip);																//adds Name label to pCenter
         pCenter.add(ZipInput);															//adds Phone text field to pCenter
         pCenter.add(lblBirthday);																//adds Name label to pCenter
         pCenter.add(BirthdayInput);															//adds Phone text field to pCenter
         pCenter.add(lblGradDate);																//adds Name label to pCenter
         pCenter.add(GradDateInput);															//adds Phone text field to pCenter
         pCenter.add(lblGpa);																//adds Name label to pCenter
         pCenter.add(GpaInput);															//adds Phone text field to pCenter
         pCenter.add(lblCreditHours);																//adds Name label to pCenter
         pCenter.add(CreditHoursInput);															//adds Phone text field to pCenter
         
         Panel pSouth = new Panel();														//creates pSouth panel
         pSouth.add(clickPrevious);															//adds previous button
         pSouth.add(clickSave);																//adds save button
         pSouth.add(clickDelete);															//adds delete button
         pSouth.add(clickNext);																//adds next button		
         
         p.add(pCenter, BorderLayout.CENTER);												//adds pCenter panel to container
         p.add(pSouth, BorderLayout.SOUTH);														//adds pSouth panel to container
         
         clickPrevious.addActionListener(this);												//assigns ActionListener to previous button
         this.pack();	
         this.setVisible(true);	
         
         clickSave.addActionListener(this);													//assigns ActionListener to save button
         this.pack();	
         this.setVisible(true);	
         
         clickDelete.addActionListener(this);												//assigns ActionListener to delete button
         this.pack();
         this.setVisible(true);
         
         clickNext.addActionListener(this);													//assigns ActionListener to next button
         this.pack();
    }//end newGUI

	public void actionPerformed(ActionEvent e) {
//        if (e.getSource() == clickSave){													//if save button is clicked, save unless there is nothing to save
//      		String x = NameInput.getText();
//      		String y = AddressInput.getText();
//      		String z = PhoneInput.getText();
//      		
//      		if(x.equals("")){																//if name is blank
//      			JOptionPane.showMessageDialog(null, "Name must be entered!");				//display error message
//      		}//end else if
//      		else if(y.equals("")){															//if address is blank
//      			JOptionPane.showMessageDialog(null, "Address must be entered!");			//display error message
//      		}//end else if		
//      		else if(z.equals("")){															//if phone number is blank
//      			JOptionPane.showMessageDialog(null, "Phone Number must be entered!");		//display error message
//      		}//end else if
//      		else{																			//if all items are entered	
//    		Name.add(NameInput.getText());														//updates list with Name
//    		Address.add(AddressInput.getText());												//updates list with Address
//    		Phone.add(PhoneInput.getText());													//updates list with Phone
//			WriteFile();																		//calls WriteFile to save to the text file
//    		counter = Name.size() - 1;															//takes counter to last record of list															
//    		JOptionPane.showMessageDialog(null, "Save is complete!");							//display message to confirm save to user
//       		}//end else
//      	 	
//      	}//end if
//    	
//    	else if (e.getSource() == clickDelete){													//if delete is clicked, delete unless there is nothing to delete	
//    		if(counter >= 0){																	//if the counter is greater than 0
//    			Name.remove(counter);															//remove name from name list
//    			Address.remove(counter);														//remove address from address list
//    			Phone.remove(counter);	  														//remove phone number from phone list
//    			WriteFile();																	//deletes record from text file
//				if (counter == Name.size()){                            	                    //if counter was last record
//					if (Name.size() == 0)                                   	                //if size is empty then counter becomes -1
//						counter = -1;
//					else
//						counter = counter - 1;												    //sets counter to previous record
//				}  				
//    			updateScreen();																	//call update method to display blank text fields
//    			JOptionPane.showMessageDialog(null, "Entry has been Deleted!");					//display message to confirm save to user
//    		}//end if
//    		else {			
//    			BlankGUI();																		//if counter is not greater than 0
//    			JOptionPane.showMessageDialog(null, "There are no records!");					//display error message to user
//    		}//end else
//    	}//end else if
//    	
//    	else if (e.getSource() == clickPrevious){												//if previous is clicked, go to previous unless there is no previous
//    		if(counter > 0){																	//if counter is greater than 0
//    			counter--;																		
//        		updateScreen();																	//call update method to display blank form
//    		}    		
//    		else
//    			JOptionPane.showMessageDialog(null, "This is the first record");				//display message to user
//    	}//end else if
//    	
//    	else if (e.getSource() == clickNext){													//if next is clicked, go to next unless there is nothing next.
		
		if (e.getSource() == clickNext){
			FirstNameInput.setText(students[counter].firstName);
			
		}
		
		
		
//    		if(counter > -1){																	//if the arrayList is not empty
//    																						
//    			if (counter<Name.size() - 1){													//if we are not at the end of the ArrayList, go to next element
//    				counter++;																	//go to next record
//    				updateScreen();
//    			}//end if
//    			else JOptionPane.showMessageDialog(null, "This is the last record!");			//else display message to let user know we are at the end
//    	
//    		}//end if		
//    		else JOptionPane.showMessageDialog(null, "There are no records!");					//display message to let user know there are no records
//    	
//    	}//end else if
//    	
//    	else JOptionPane.showMessageDialog(null, "Error!");		
	}

	public void readFile(){
		for(int j = 0; j<50; j++){
			students[j] = new Student();
		}
		
		BufferedReader readBuffer;
		try {
			readBuffer = new BufferedReader(new FileReader("Students.txt"));
	
			String strRead;
			int p = 0;
			while((strRead = readBuffer.readLine())!=null){								
				//System.out.println("readFile first step in while");
				String line1 = (String) strRead;	
//				System.out.println(line1);
//				System.out.println(line1);
//				System.out.println("readFile first step in while2");
				students[p].parse(line1);
//				System.out.println("readFile after first parse call");
//				System.out.println(line[p]);
				p++;
				
			}
		}	
			catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
	
	private void updateScreen() {															//Updates text fields with requested data
//		//updates screen with info
//		if(counter == -1){
//	//		BlankGUI();
//		}
//		
//		else {FirstNameInput.setText((String) students[counter].firstName);								//uses counter for ArrayList
//		
		FirstNameInput.setText((String) students[counter].firstName);	
		LastNameInput.setText((String) students[counter].lastName);
		AddressLine1Input.setText((String) students[counter].addressLine1);
		AddressLine2Input.setText((String) students[counter].addressLine2);
		CityInput.setText((String) students[counter].city);
		StateInput.setText((String) students[counter].state);
		ZipInput.setText((String) students[counter].zip);
		birthday.month.setText((String) students[counter].zip);		//birthday cannot be resolved? 
		ZipInput.setText((String) students[counter].zip);
		ZipInput.setText((String) students[counter].zip);
		ZipInput.setText((String) students[counter].zip);
		ZipInput.setText((String) students[counter].zip);
	
		
		
		
		
//			AddressInput.setText((String) Address.get(counter));
//			PhoneInput.setText((String) Phone.get(counter));
		}
	

}

I corrected line 233

BirthdayInput.setText((String) students[counter].birthday.month);

Still does not recognize birthday

Well, where is birthday declared?

I have cleaned up some of the my code. Line 233 of the StudentContainer class is not recognizing the birthday object (initialized in Student Class, part of Date class).

Does anyone know of a solution?

import java.io.*; 
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class StudentContainer extends JFrame implements ActionListener {	
    public int counter = 0;
	
	private static final long serialVersionUID = 1L;
		final int num_students = 50;						
		Student students[] = new Student[num_students];	
		String line[] = new String [50];
		
		JLabel lblFirstName = new JLabel(" First Name:");									//creates lbl
	    JLabel lblLastName = new JLabel(" Last Name:");										//creates lbl
		JLabel lblAddressLine1 = new JLabel(" Address Line 1:");							//creates lbl
	    JLabel lblAddressLine2 = new JLabel(" Address Line 2:");							//creates lbl
	    JLabel lblCity = new JLabel(" City:");												//creates lbl
	    JLabel lblState = new JLabel(" State:");											//creates lbl
	    JLabel lblZip = new JLabel(" Zip Code:");											//creates lbl
	    JLabel lblBirthday = new JLabel(" Date of Birth:");									//creates lbl
	    JLabel lblGradDate = new JLabel(" Graduation Date:");								//creates lbl
	    JLabel lblGpa = new JLabel(" GPA:");												//creates lbl
	    JLabel lblCreditHours = new JLabel(" Credit Hours:");								//creates lbl
	    
	    JButton clickSave = new JButton("Save");											//creates Save button
	    JButton clickPrevious = new JButton("Previous");									//creates Previous button
	    JButton clickNext = new JButton("Next");											//creates Next button
	    JButton clickDelete = new JButton("Delete");										//creates Delete button
	    JTextField FirstNameInput = new JTextField("");										//creates NameInput text field
	    JTextField LastNameInput = new JTextField("");										
	    JTextField AddressLine1Input = new JTextField("");	
	    JTextField AddressLine2Input = new JTextField("");
	    JTextField CityInput = new JTextField("");
	    JTextField StateInput = new JTextField("");
	    JTextField ZipInput = new JTextField("");
	    JTextField BirthdayInput = new JTextField("");
	    JTextField GradDateInput = new JTextField("");
	    JTextField GpaInput = new JTextField("");
	    JTextField CreditHoursInput = new JTextField("");
	
	public StudentContainer() {
        super("STUDENT DATABASE");															//container header is "STUDENT DATABASE"						
        newGUI();																			//calls newGUI to initialize GUI 
        readFile();
        updateScreen();
	} // end constructor{

	private void newGUI(){
		 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);								//exit on close statement
         Container p = this.getContentPane();												//declare container p
         p.setLayout(new BorderLayout());													//set p to BorderLayout
         Panel pCenter = new Panel();														//creates pCenter panel
         	
         pCenter.setLayout(new GridLayout(0,2));											//initiates grid layout
         pCenter.add(lblFirstName);															//adds Name label to pCenter
         pCenter.add(FirstNameInput);														//adds Phone text field to pCenter
         pCenter.add(lblLastName);															//adds Name label to pCenter
         pCenter.add(LastNameInput);														//adds Phone text field to pCenter
         pCenter.add(lblAddressLine1);														//adds Name text field to pCenter	
         pCenter.add(AddressLine1Input);													//adds Phone text field to pCenter
         pCenter.add(lblAddressLine2);														//adds Name label to pCenter
         pCenter.add(AddressLine2Input);													//adds Phone text field to pCenter
         pCenter.add(lblCity);																//adds Name label to pCenter
         pCenter.add(CityInput);															//adds Phone text field to pCenter
         pCenter.add(lblState);																//adds Name label to pCenter
         pCenter.add(StateInput);															//adds Phone text field to pCenter
         pCenter.add(lblZip);																//adds Name label to pCenter
         pCenter.add(ZipInput);																//adds Phone text field to pCenter
         pCenter.add(lblBirthday);															//adds Name label to pCenter
         pCenter.add(BirthdayInput);														//adds Phone text field to pCenter
         pCenter.add(lblGradDate);															//adds Name label to pCenter
         pCenter.add(GradDateInput);														//adds Phone text field to pCenter
         pCenter.add(lblGpa);																//adds Name label to pCenter
         pCenter.add(GpaInput);																//adds Phone text field to pCenter
         pCenter.add(lblCreditHours);														//adds Name label to pCenter
         pCenter.add(CreditHoursInput);														//adds Phone text field to pCenter
         
         Panel pSouth = new Panel();														//creates pSouth panel
         pSouth.add(clickPrevious);															//adds previous button
         pSouth.add(clickSave);																//adds save button
         pSouth.add(clickDelete);															//adds delete button
         pSouth.add(clickNext);																//adds next button		
         
         p.add(pCenter, BorderLayout.CENTER);												//adds pCenter panel to container
         p.add(pSouth, BorderLayout.SOUTH);													//adds pSouth panel to container
         
         clickPrevious.addActionListener(this);												//assigns ActionListener to previous button
         this.pack();	
         this.setVisible(true);	
         
         clickSave.addActionListener(this);													//assigns ActionListener to save button
         this.pack();	
         this.setVisible(true);	
         
         clickDelete.addActionListener(this);												//assigns ActionListener to delete button
         this.pack();
         this.setVisible(true);
         
         clickNext.addActionListener(this);													//assigns ActionListener to next button
         this.pack();
    }//end newGUI

	public void actionPerformed(ActionEvent e) {
//        if (e.getSource() == clickSave){													//if save button is clicked, save unless there is nothing to save
//      		String x = NameInput.getText();
//      		String y = AddressInput.getText();
//      		String z = PhoneInput.getText();
//      		
//      		if(x.equals("")){																//if name is blank
//      			JOptionPane.showMessageDialog(null, "Name must be entered!");				//display error message
//      		}//end else if
//      		else if(y.equals("")){															//if address is blank
//      			JOptionPane.showMessageDialog(null, "Address must be entered!");			//display error message
//      		}//end else if		
//      		else if(z.equals("")){															//if phone number is blank
//      			JOptionPane.showMessageDialog(null, "Phone Number must be entered!");		//display error message
//      		}//end else if
//      		else{																			//if all items are entered	
//    		Name.add(NameInput.getText());														//updates list with Name
//    		Address.add(AddressInput.getText());												//updates list with Address
//    		Phone.add(PhoneInput.getText());													//updates list with Phone
//			WriteFile();																		//calls WriteFile to save to the text file
//    		counter = Name.size() - 1;															//takes counter to last record of list															
//    		JOptionPane.showMessageDialog(null, "Save is complete!");							//display message to confirm save to user
//       		}//end else
//      	 	
//      	}//end if
//    	
//    	else if (e.getSource() == clickDelete){													//if delete is clicked, delete unless there is nothing to delete	
//    		if(counter >= 0){																	//if the counter is greater than 0
//    			Name.remove(counter);															//remove name from name list
//    			Address.remove(counter);														//remove address from address list
//    			Phone.remove(counter);	  														//remove phone number from phone list
//    			WriteFile();																	//deletes record from text file
//				if (counter == Name.size()){                            	                    //if counter was last record
//					if (Name.size() == 0)                                   	                //if size is empty then counter becomes -1
//						counter = -1;
//					else
//						counter = counter - 1;												    //sets counter to previous record
//				}  				
//    			updateScreen();																	//call update method to display blank text fields
//    			JOptionPane.showMessageDialog(null, "Entry has been Deleted!");					//display message to confirm save to user
//    		}//end if
//    		else {			
//    			BlankGUI();																		//if counter is not greater than 0
//    			JOptionPane.showMessageDialog(null, "There are no records!");					//display error message to user
//    		}//end else
//    	}//end else if
//    	
//    	else if (e.getSource() == clickPrevious){												//if previous is clicked, go to previous unless there is no previous
//    		if(counter > 0){																	//if counter is greater than 0
//    			counter--;																		
//        		updateScreen();																	//call update method to display blank form
//    		}    		
//    		else
//    			JOptionPane.showMessageDialog(null, "This is the first record");				//display message to user
//    	}//end else if
//    	
//    	else if (e.getSource() == clickNext){													//if next is clicked, go to next unless there is nothing next.
		
		if (e.getSource() == clickNext){
			FirstNameInput.setText(students[counter].firstName);
			
		}
		
		
		
//    		if(counter > -1){																	//if the arrayList is not empty
//    																						
//    			if (counter<Name.size() - 1){													//if we are not at the end of the ArrayList, go to next element
//    				counter++;																	//go to next record
//    				updateScreen();
//    			}//end if
//    			else JOptionPane.showMessageDialog(null, "This is the last record!");			//else display message to let user know we are at the end
//    	
//    		}//end if		
//    		else JOptionPane.showMessageDialog(null, "There are no records!");					//display message to let user know there are no records
//    	
//    	}//end else if
//    	
//    	else JOptionPane.showMessageDialog(null, "Error!");		
	}

	public void readFile(){
		for(int j = 0; j<50; j++){
			students[j] = new Student();
		}
		
		BufferedReader readBuffer;
		try {
			readBuffer = new BufferedReader(new FileReader("Students.txt"));
	
			String strRead;
			int p = 0;
			while((strRead = readBuffer.readLine())!=null){								
				//System.out.println("readFile first step in while");
				String line1 = (String) strRead;	
//				System.out.println(line1);
//				System.out.println(line1);
//				System.out.println("readFile first step in while2");
				students[p].parse(line1);
//				System.out.println("readFile after first parse call");
//				System.out.println(line[p]);
				p++;
				
			}
		}	
			catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
	
	private void updateScreen() {															//Updates text fields with requested data
//		//updates screen with info
//		if(counter == -1){
//	//		BlankGUI();
//		}
//		
//		else {FirstNameInput.setText((String) students[counter].firstName);								//uses counter for ArrayList
//		
		FirstNameInput.setText(students[counter].firstName);	
		LastNameInput.setText(students[counter].lastName);
		AddressLine1Input.setText(students[counter].addressLine1);
		AddressLine2Input.setText(students[counter].addressLine2);
		CityInput.setText(students[counter].city);
		StateInput.setText(students[counter].state);
		ZipInput.setText(students[counter].zip);
		//BirthdayInput.setText(students[counter].birthday.month);		//birthday cannot be resolved? 
		//GradDateInput.setText(students[counter].graduation.month);
		GpaInput.setText(students[counter].gpa);
		CreditHoursInput.setText(students[counter].creditHours);
	
		
		
		
		
//			AddressInput.setText((String) Address.get(counter));
//			PhoneInput.setText((String) Phone.get(counter));
		}
	

}
public class Student {
		
		String firstName = " ";
		String lastName = " ";
		String addressLine1 = " ";
		String addressLine2 = " ";
		String city = " ";
		String state = " ";
		String zip = " ";
		String bdayMonth = " ";
		String bdayDay = " ";
		String bdayYear = " ";
		String gdayMonth = " ";
		String gdayDay = " ";
		String gdayYear = " ";
		String gpa = " ";
		String creditHours = " ";
		
	public Student() {						//What should the constructor be doing?
		
	}
	
	public void parse(String line){
		Date birthday = new Date();
		Date graduation = new Date();
		Address address = new Address();
		
		String delim = ",";							//splits input from text file
		String tokens[] = line.split(delim);

		firstName = tokens[0];
		lastName = tokens[1];
		address.line1 = tokens[2];
		address.line2 = tokens[3];
		address.city = tokens[4];
		address.state = tokens[5];
		address.zip = tokens[6];
		birthday.month = tokens[7];
		birthday.day = tokens[8];
		birthday.year = tokens[9];
		graduation.month = tokens[10];
		graduation.day = tokens[11];
		graduation.year = tokens[12];
		gpa = tokens[13];
		creditHours = tokens[14];
		
//		System.out.println(firstName);
//		System.out.println(lastName);
//		System.out.println(addressLine1);
//		System.out.println(addressLine2);
//		System.out.println(city);
//		System.out.println(state);
//		System.out.println(zip);
//		System.out.println(birthday.month);
//		System.out.println(birthday.day);
//		System.out.println(birthday.year);
//		System.out.println(graduation.month);
//		System.out.println(graduation.day);
//		System.out.println(graduation.year);
//		System.out.println(gpa);
//		System.out.println(creditHours);
//		
		
	}
	
}
public class Date {
	
	public String month;
	public String day;
	public String year;
	

//	public Date() {
//	
//	} // end constructor	
//	
	
	
	
}

Jon, it's declared in the Student class.

Holy cow I got it....

I originally declared the object in the parse method. Moved it to the top and it works like a charm. Thanks Jon.

Glad it's working.

Now I should point out that all those public fields are a bad idea, in terms of object-oriented programming. In a nutshell, you want each object to control its own data. If you must allow an object to reach into another object and change its data, you should control that access by providing a setter method. Generally, you shouldn't even do that. For example, in your Date class, you should just provide a constructor which takes the required fields as parameters and sets up the date.

One of the ways in which this helps you is this: if you find that you have a bug where Dates are changing and you don't know why, you can easily add a println to a setMonth method which would give you an alert when the method is called. If it's a public field, you have no way of figuring out when it's changed except by going over all of the code looking for suspect lines.
(this also allows you to validate the parameters, so you don't end up with February 31 as a date, and to take action when a date is changed, like move an item on a calendar, and there's plenty of other reasons which can wait for now. Don't use public fields.)

I have been trying add the fields to the constructor, but it does not seem to work. Below is an example. Maybe I am using the constructor incorrectly?

With the way it is set up below, it does not compile. If I comment out the constructor, and uncomment the public fields, it works fine.

public class Date {
//	
//	public String month;
//	public String day;
//	public String year;
	

	public Date() {
		String month;
		String day;
		String year;

	} // end constructor	

	
	
	
}

This also does not work.

public class Date {
//	
//	public String month;
//	public String day;
//	public String year;
//	

	public Date(String x, String y, String z) {
		String month = x;
		String day = y;
		String year = z;

	} // end constructor	

	private String getMonth(){
		String x = month;
		return x;
	}
}

This seems to be working. Is this better?

public class Date {

	String month;
	String day;
	String year;
	
	public Date(){
	}
	
	public Date(String x, String y, String z) {
		month = x;
		day = y;
		year = z;
	} // end constructor	
}

Yes, that's right. What's happening here is that you're declaring that Date as a class has three fields, which are available throughout the class. Every Date object has a month, a day, and a year. When you declared them inside the constructor, you were declaring local variables, which expire at the end of the method that declares them.

Standard format for this would be

public Date(){
	}
	
	public Date(String month, String day, String year) {
		this.month = month;
		this.day = day;
		this.year = year;
	} // end constructor

"this" means "the object we're currently in", so this.month refers to the field, not to the local variable. Do some reading on shadowing, variable scope, and stuff like that. This here is definitive, but possibly a bit tough going. Give it a try, though, see what you can get out of it.

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.