non-static variable calcGrade cannot be referenced from a static context

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blknmld69
    New Member
    • Feb 2010
    • 69

    #1

    non-static variable calcGrade cannot be referenced from a static context

    I am working on a major project. I keep getting the error non-static variable calcGrade cannot be referenced from a static context calcGrade.stude ntList[s] = new Student();

    Code:
    import java.util.*;  
       import java.io.*;
       import javax.swing.*; 
       import java.awt.*; 
       import java.awt.event.*; 
     
        public class GradeCalculator extends JFrame 
        { 
          private static final int WIDTH = 550; 
          private static final int HEIGHT = 430; 
             
          private static final int MAX_NUMBER_OF_STUDENTS = 20; 
             
                    // instance variables 
          String Student[] = new String[MAX_NUMBER_OF_STUDENTS]; 
          private int noOfStudents; 
          private double score, tst1, tst2, tst3; 
          private double  classAvg, stAvg, totalScore; 
          private int displayedStudentIndex = 0; 
          private char ltrGrade; 
          private String stName; 
             
                    // This area is for the GUI components 
                    // Each item that will be displayed will 
                    // have a label and a textfield, (L) and (TF),  
                    // respectively. 
          private JLabel stNameL, tst1L, tst2L, tst3L, 
                                            classAvgL, stAvgL, headingL; 
          private JTextField stNameTF, tst1TF, tst2TF, tst3TF; 
          private JTextArea classAvgTA, stAvgTA; 
          private JButton exitB, nextB, prevB, calcGrade; 
             
          private ButtonHandler bHandler; 
             
           public GradeCalculator() 
           { 
             setTitle("Grade Calculator");           // set's the title 
             setSize(WIDTH, HEIGHT);                 // set the window size 
             Container pane = getContentPane();      // get the container 
             pane.setLayout(null);                   // set the container's layout to null 
                             
             bHandler = new ButtonHandler();         // instantiate the button event handler 
          
                    // instantiate the labels 
             headingL = new JLabel("STUDENT RECORD"); 
             stNameL = new JLabel("Studen Name", SwingConstants.RIGHT); 
             tst1L = new JLabel("Test 1", SwingConstants.LEFT); 
             tst2L = new JLabel("Test 2", SwingConstants.LEFT); 
             tst3L = new JLabel("Test 3", SwingConstants.LEFT); 
             stAvgL = new JLabel("Student Average     " 
                                                    + "\n"   + "Class Average"); 
                    //instantiate the text fields 
             stNameTF = new JTextField(65); 
             tst1TF = new JTextField(10); 
             tst2TF = new JTextField(10); 
             tst3TF = new JTextField(10); 
                      
                    // instantiate the text area 
             classAvgTA = new JTextArea(6, 20); 
             classAvgTA.setAutoscrolls(true); 
          
                    // instantiate the buttons and register the listener 
             exitB = new JButton("Exit"); 
             exitB.addActionListener(bHandler); 
          
             nextB = new JButton("Next"); 
             nextB.addActionListener(bHandler); 
          
             prevB = new JButton("Previous"); 
             prevB.addActionListener(bHandler); 
          
             calcGrade = new JButton("Calc Grade"); 
             calcGrade.addActionListener(bHandler); 
          
                    // set the size of the labels, text fields, and buttons 
                    
             headingL.setSize(200, 30); 
             stNameL.setSize(100, 30); 
             stNameTF.setSize(100, 30); 
             tst1L.setSize(100, 30); 
             tst1TF.setSize(100, 30); 
             tst2L.setSize(120, 30); 
             tst2TF.setSize(100, 30); 
             tst3L.setSize(100, 30); 
             tst3TF.setSize(100, 30); 
             classAvgTA.setSize(370, 120); 
             calcGrade.setSize(100, 30); 
             prevB.setSize(100, 30);
             nextB.setSize(100, 30); 
             exitB.setSize(100, 30); 
                       
                    //set the location of the labels, text fields, 
                    //and buttons 
             headingL.setLocation(220, 10); 
             stNameL.setLocation(20, 50); 
             stNameTF.setLocation(120, 50); 
             tst1L.setLocation(20, 100); 
             tst1TF.setLocation(120, 100); 
             tst2L.setLocation(300, 50); 
             tst2TF.setLocation(420, 50); 
             tst3L.setLocation(300, 100); 
             tst3TF.setLocation(420, 100); 
             classAvgTA.setLocation(70, 230); 
             prevB.setLocation(120, 370); 
             exitB.setLocation(220, 370); 
             nextB.setLocation(320, 370); 
             calcGrade.setLocation(420, 370); 
          
                    //add the labels, text fields, and buttons to the pane 
             pane.add(headingL); 
             pane.add(stNameL); 
             pane.add(stNameTF); 
             pane.add(tst1L); 
             pane.add(tst1TF); 
             pane.add(tst2L); 
             pane.add(tst2TF); 
             pane.add(tst3L); 
             pane.add(classAvgTA); 
             pane.add(calcGrade); 
             pane.add(prevB); 
             pane.add(exitB); 
             pane.add(nextB); 
                       
             setVisible(true); 		//show the window 
             setDefaultCloseOperation(EXIT_ON_CLOSE); 
             System.exit(0); 
          } 
         	   
       
       
          public static void main (String [] args)
          { 
             
    			File file = new File("AcademicGrades.txt");
    			Scanner keyboard = new Scanner(System.in);
    			Scanner inputFile = new Scanner(file); 
    			                             
          
             for (int s = 0; s < MAX_NUMBER_OF_STUDENTS; s++) 
    			{
                calcGrade.studentList[s] = new Student(); 
             
             	gradeCalc.noOfStudents = 
                            inputFile.nextInt();       // get the number of students 
             	gradeCalc.score = 
                            inputFile.nextDouble();    // get the student's scores 
             
            	   gradeCalc.getStudentData(inputFile); 
             	gradeCalc.displayGradeAverage(0); 
             }
           }
                            // get the student data from file 
       							
       		
           public void getStudentData(Scanner inputFile) 
           { 
             System.out.println("Grade Calculator is getting information..."); 
             System.out.println("One Moment Please"); 
           } 
           private class ButtonHandler implements ActionListener 
           { 
              public void actionPerformed (ActionEvent e) 
              { 
                if (e.getActionCommand().equals("Previous")) 
                   if (displayedStudentIndex > 0) 
                      displayGradeAverage(displayedStudentIndex - 1); 
                   else 
                      displayGradeAverage(displayedStudentIndex); 
                else if (e.getActionCommand().equals("Next")) 
                   if (displayedStudentIndex + 1 < noOfStudents) 
                      displayGradeAverage(displayedStudentIndex + 1); 
                   else 
                      displayGradeAverage(displayedStudentIndex); 
                else if (e.getActionCommand().equals("Calc Grade")) 
                   displayGradeAverage(0); 
                else 
                   System.exit(0); 
             } 
           } 
       
           public void displayGradeAverage(int stName) 
           { 
    		 	int studentList;
             displayedStudentIndex = stName; 
             String strName = ""; 
             boolean classAvg = studentList[(int) (totalScore / noOfStudents)].getClassAverage(); 
             stNameTF.setText(studentList[stName].getFirstName() + " " 
                												+ studentList[stName].getLastName());
             stAvgTA.setText(""+studentList[(int) (totalScore / 5.0)].getStudentAverage());
             classAvgTA.setText(""+studentList[(int) (totalScore / noOfStudents)].getClassAverage()); 
           }
         }
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    The reason its giving you troubles is that your student loop is in the main method at line 131:
    Code:
    public static void main (String [] args)
    Main, you will notice, is a static method, and thus is not associated with any specific object. Whereas the attribute calcGrade is an attribute of the GradeCalculator class.

    The problem being that you never actually instantiate a GradeCalculator object in your code. You just start banging on object attributes in main without creating any object at all.

    What I think you need to do is move all the code from main into getStudentData, and insert code in main to instantiate a GradeCalculator and run the awt event loop.

    First, though, you have to get through your compilation problems. Work those out, then the rest should be easy.

    If you're in doubt, comment out sections of code until you can display a minimal, working window, then re-enable code from there.

    Comment

    • Subin Ninan
      New Member
      • Sep 2010
      • 91

      #3
      Static methods cannot access non-static variables. Static fields, methods are initialized before non-static fields.

      Comment

      • blknmld69
        New Member
        • Feb 2010
        • 69

        #4
        No Im gettn this cannot find symbols:
        Code:
           import javax.swing.*; 		//needed for swing classes
           import java.awt.event.*; 	// needed for the action listener
           import java.awt.*;			//needed for the boader layout class
           import java.io.*; 			//need for the file and IOException
           import java.util.Scanner;	// needed for the scanner class
           import java.util.List;  	// needed for the  arraylist
           import java.util.Arrays;  	//needed for the arraylict class
        	
        	/**
        		Constructor
        	*/
           
        
           public class project3 extends JFrame 
           {
           	
              public gradeTypePanel gradeType; //to reference the the grade Type Panel with the radio buttons
              public namePanel name; // to reference the name panel with the name text field
              public testPanel test;// to reference the test panel with the 3 test grade fields
              public resultPanel result;// to reference the result panel with the result text field
              private JPanel panel; // to reference the a  panel
              private JButton calcButton; // creats the Button named calcButton
              private JButton prevButton; // creats the Button named prevButton
              private JButton nextButton; // creats the Button named nextButton
              private JButton saveButton; // creats the Button named saveButton
              private JPanel buttonPanel; // to  reference the panel where all the buttons go
              public openFile open;// to reference the open file class
              public int masterIndex = -1; // this controls all the of the indexes of all 4 arrays
              
           	// constructs the window
              public project3()
              {
                 // sets the title
                 setTitle("Grade Program");
                 // set what happens when the exit button is clicked
                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 // sets the border layout
                 setLayout(new BorderLayout());
                 //calls the resultPanel class and names it result
                 result = new resultPanel();
                 //calls the gradeTypePanel and names it gradeType
                 gradeType = new gradeTypePanel();
                 //calls the namePanel and names it name
                 name = new namePanel();
                 //calls the testPanel and names it test
                 test = new testPanel();
                 
              	//builds the button panel
                 buildButtonPanel();
              	
              	//adds all of the panels to the north, south, east, west, and center panels
                 add(name, BorderLayout.NORTH);  
                 add(gradeType, BorderLayout.WEST);
                 add(test, BorderLayout.CENTER);
                 add(buttonPanel, BorderLayout.EAST);
                 add(result, BorderLayout.SOUTH);
                 
              	// packs all the panels togather
                 pack();
              	
              	//makes the window visable
                 setVisible(true);
              }
              
              //builds the panel for the buttons
              private void buildButtonPanel()
              {
              
                 buttonPanel = new JPanel();
                         
                 
                 prevButton = new JButton("Prev");// adds the text prev to the prevButton
                 calcButton = new JButton("Calc Grade");// adds the text Calc Grade to the calcButton
                 nextButton = new JButton("Next");// adds the text Next to the nextButton 
                 saveButton = new JButton("Save");// adds the text Save to the saveButton
                 
              	
                 calcButton.addActionListener(new CalcButtonListener());// calls the  ActionListener for the calcButton
                 prevButton.addActionListener(new PrevButtonListener());// calls the  ActionListener for the prevButto
                 nextButton.addActionListener(new NextButtonListener());// calls the  ActionListener for the nextButton
                 saveButton.addActionListener(new SaveButtonListener());// calls the  ActionListener for the addButton
              
                 
                 buttonPanel.add(prevButton);// adds the prevButton to the panel
                 buttonPanel.add(saveButton);// adds the saveButton to the panel
                 buttonPanel.add(nextButton);// adds the nextButton to the panel
                 buttonPanel.add(calcButton);// adds the calcButton to the panel
                 
                 
              }
               //creats actionlistener for the Calc Grade button
              private class CalcButtonListener implements ActionListener
              {
              	// executes when the button is clickd 
                 public void actionPerformed(ActionEvent e)
                 {
                   //if masterIndex = -1 then you are not at index 0 of the arrays so you cant calculate
                    if(masterIndex != -1)
                    {
                       String nameText;// to save the text in the name text field
                       nameText = name.txtname.getText();//gets the text form name text field
                       String grade1;// to save the text in the test1 text field
                       grade1 = test.test1TextFild.getText();//gets the text form test1 text field
                       String grade2;// to save the text in the test2 text field
                       grade2 = test.test2TextFild.getText();//gets the text form test2 text field
                       String grade3;// to save the text in the test3 text field
                       grade3 = test.test3TextFild.getText();//gets the text form test3 text field
                    	
                       //if the text in all 3 text field are  <= 100 calculate
                       if(Double.parseDouble(grade1) <= 100 && Double.parseDouble(grade2) <= 100 && Double.parseDouble(grade3) <= 100)
                       {
                          //convert and add all 3 grades together
                          double totalGrade = Double.parseDouble(grade1) + Double.parseDouble(grade2) + Double.parseDouble(grade3); 
                       	//get the the avarage
                          double NumberGrade = (totalGrade / 300) * 100;
                       	//to hold the letter grade
                          String letterGrade;
                          //if the alphabetical radio button is Selected get the letter grade
                          if(gradeType.alphabetical.isSelected())
                          
                          {
                          
                          
                             if(NumberGrade >= 90)
                             {
                                //sets letter grade to A
                                letterGrade = "A";
                             	//display the result in the result text field
                                result.textResult.setText(letterGrade);
                             
                             }
                             else if(NumberGrade >= 80)
                             {
                                //sets letter grade to B
                                letterGrade = "B";
                             	//display the result in the result text field
                                result.textResult.setText(letterGrade);
                             }
                             else if(NumberGrade >= 70)
                             {
                                //sets letter grade to C
                                letterGrade = "C";
                             	//display the result in the result text field
                                result.textResult.setText(letterGrade);
                             }
                             else if(NumberGrade >= 60)
                             {
                                //sets letter grade to D
                                letterGrade = "D";
                             	//display the result in the result text field
                                result.textResult.setText(letterGrade);
                             }
                             else if(NumberGrade < 60)
                             {
                                //sets letter grade to F
                                letterGrade = "F";
                             	//display the result in the result text field
                                result.textResult.setText(letterGrade);
                             }
                          
                          
                          }
                       	//if numerical is Selected get the number grade
                          if(gradeType.numerical.isSelected())
                          {
                             //display the result in the result text field
                             result.textResult.setText(Double.toString(NumberGrade));
                          }
                       }
                       else
                       {
                          //displays error message when all grades are not <= 100
                          JOptionPane.showMessageDialog(null, "All Grades must be less than or equal to (100)");
                       }
                    }
                    else
                    {
                    	//shows error message when masterIndex == -1
                       JOptionPane.showMessageDialog(null, "Please click (Next) first");
                    }
                 	
                 }										
              }
              //creats actionlistener for the Calc prev button
              private class PrevButtonListener implements ActionListener
              {
              	// executes when the button is clickd gets the input and calcilates it
                 public void actionPerformed(ActionEvent e)
                 {
                    //if masterIndex is > 0 
                    if(masterIndex > 0)
                    { 
                       //decrament masterIndex by 1
                       masterIndex--;
                    	
                       String[] nameList;//to store the array
                       String[] test1;//to store the array
                       String[] test2;//to store the array
                       String[] test3;//to store the array
                    
                    
                       try 
                       {
                       
                          nameList = open.getArrayName();//get the nameList array from the class
                          test1 = open.getArrayTest1();//get the test1 array from the class
                          test2 = open.getArrayTest2();//get the test2 array from the class
                          test3 = open.getArrayTest3();//get the test3 array from the class
                          name.txtname.setText(nameList[masterIndex]);//populate the txtname text field with the array
                          test.test1TextFild.setText(test1[masterIndex]);//populate the test1 text field with the array
                          test.test2TextFild.setText(test2[masterIndex]);//populate the test2 text field with the array
                          test.test3TextFild.setText(test3[masterIndex]);//populate the test3 text field with the array
                       
                       } 
                          catch (IOException x)
                          { 
                             //displays an error message
                             System.out.println("Error");
                          } 
                    }
                    
                    else
                    {
                       JOptionPane.showMessageDialog(null, "There are no previous students");
                    }
                 
                 }								
              }
              //creats actionlistener for the next button
              private class NextButtonListener implements ActionListener
              {
              	// executes when the button is clickd 
                 public void actionPerformed(ActionEvent e)
                 {
                    //incrament masterIndex by 1
                    masterIndex++;
                    
                    String[] nameList;//to store the array
                    String[] test1;//to store the array
                    String[] test2;//to store the array
                    String[] test3;//to store the array
                    
                    
                    try 
                    {
                       
                       nameList = open.getArrayName();//get the nameList array from the class
                       test1 = open.getArrayTest1();//get the test1 array from the class
                       test2 = open.getArrayTest2();//get the test2 array from the class
                       test3 = open.getArrayTest3();//get the test3 array from the class
                       name.txtname.setText(nameList[masterIndex]);//populate the txtname text field with the array
                       test.test1TextFild.setText(test1[masterIndex]);//populate the test1 text field with the array
                       test.test2TextFild.setText(test2[masterIndex]);//populate the test2 text field with the array
                       test.test3TextFild.setText(test3[masterIndex]);//populate the test3 text field with the array
                    
                                   	
                    } 
                       catch (IOException x)
                       { 
                           //displays an error message
                          System.out.println("Error");
                       } 
                 }
                 									
              }
              //creats actionlistener for the save button
              private class SaveButtonListener implements ActionListener
              {
              	// executes when the button is clickd 
                 public void actionPerformed(ActionEvent e)
                 {
                 	//if masterIndex = -1 then you are not at index 0 of the arrays so you cant calculate
                    if(masterIndex != -1)
                    {
                    
                       try 
                       {
                       
                          String[] nameList;//to store the array
                          String[] test1;//to store the array
                          String[] test2;//to store the array
                          String[] test3;//to store the array
                       
                          nameList = open.getArrayName();//get the nameList array from the class
                          test1 = open.getArrayTest1();//get the test1 array from the class
                          test2 = open.getArrayTest2();//get the test2 array from the class
                          test3 = open.getArrayTest3();//get the test3 array from the class
                       
                          String saveName;// to save the text in the name text field
                          saveName = name.txtname.getText();//gets the text form name text field
                          String saveGrade1;// to save the text in the test1 text field
                          saveGrade1 = test.test1TextFild.getText();//gets the text form test1 text field
                          String saveGrade2;// to save the text in the test2 text field
                          saveGrade2 = test.test2TextFild.getText();//gets the text form test2 text field
                          String saveGrade3;// to save the text in the test3 text field
                          saveGrade3 = test.test3TextFild.getText();//gets the text form test3 text field
                       
                          List<String> name1List = Arrays.asList(nameList); //treats the array as an arrayList 
                          List<String> test1List = Arrays.asList(test1);//treats the array as an arrayList
                          List<String> test2List = Arrays.asList(test2);//treats the array as an arrayList
                          List<String> test3List = Arrays.asList(test3);//treats the array as an arrayList
                       
                          name1List.set(masterIndex, saveName);//changes of sets the value at the masterIndex to saveName
                          test1List.set(masterIndex, saveGrade1);//changes of sets the value at the masterIndex to saveGrade1
                          test2List.set(masterIndex, saveGrade2);//changes of sets the value at the masterIndex to saveGrade2
                          test3List.set(masterIndex, saveGrade3);//changes of sets the value at the masterIndex to saveGrade3
                       
                       //opens the file for writing
                          PrintWriter outputFile = new PrintWriter("name.txt");
                       //writes the array to a text file
                          for(int index = 0; index < nameList.length; index++)
                          {
                             outputFile.println(nameList[index]);
                          }
                       //closes the file	
                          outputFile.close();
                       
                       //opens the file for writing
                          PrintWriter outputFile1 = new PrintWriter("grade1.txt");
                       //writes the array to a text file
                          for(int index1 = 0; index1 < test1.length; index1++)
                          {
                             outputFile1.println(test1[index1]);
                          }
                       //closes the file
                          outputFile1.close();
                       
                       //opens the file for writing
                          PrintWriter outputFile2 = new PrintWriter("grade2.txt");
                       //writes the array to a text file
                          for(int index2 = 0; index2 < test2.length; index2++)
                          {
                             outputFile2.println(test2[index2]);
                          }
                       //closes the file
                          outputFile2.close();
                       
                       //opens the file for writing
                          PrintWriter outputFile3 = new PrintWriter("grade3.txt");
                       //writes the array to a text file
                          for(int index3 = 0; index3 < test3.length; index3++)
                          {
                             outputFile3.println(test3[index3]);
                          }
                       //closes the file
                          outputFile3.close();
                       
                       
                       }  
                          catch (IOException x)
                          { 
                          //displays an error message
                             System.out.println("Error!");
                          }  
                    }
                    else
                    {
                    	//shows error message when masterIndex == -1
                       JOptionPane.showMessageDialog(null, "Please click (Next) first");
                    	
                    }      
                 }										
              }
           
              public static void main(String[] a)
              {
                 // calls the project2 class
                 new project2();
              	
              }
           }
        ERRORS
        ----jGRASP exec: javac -g project3.java

        project3.java:1 7: cannot find symbol
        symbol : class gradeTypePanel
        location: class project3
        public gradeTypePanel gradeType; //to reference the the grade Type Panel with the radio buttons
        ^
        project3.java:1 8: cannot find symbol
        symbol : class namePanel
        location: class project3
        public namePanel name; // to reference the name panel with the name text field
        ^
        project3.java:1 9: cannot find symbol
        symbol : class testPanel
        location: class project3
        public testPanel test;// to reference the test panel with the 3 test grade fields
        ^
        project3.java:2 0: cannot find symbol
        symbol : class resultPanel
        location: class project3
        public resultPanel result;// to reference the result panel with the result text field
        ^
        project3.java:2 7: cannot find symbol
        symbol : class openFile
        location: class project3
        public openFile open;// to reference the open file class
        ^
        project3.java:4 0: cannot find symbol
        symbol : class resultPanel
        location: class project3
        result = new resultPanel();
        ^
        project3.java:4 2: cannot find symbol
        symbol : class gradeTypePanel
        location: class project3
        gradeType = new gradeTypePanel( );
        ^
        project3.java:4 4: cannot find symbol
        symbol : class namePanel
        location: class project3
        name = new namePanel();
        ^
        project3.java:4 6: cannot find symbol
        symbol : class testPanel
        location: class project3
        test = new testPanel();
        ^
        project2.java:1 7: cannot find symbol
        symbol : class gradeTypeLabel
        location: class project2
        public gradeTypeLabel gradeType; // to reference the the grade Type Label with the radio buttons
        ^
        project2.java:1 8: cannot find symbol
        symbol : class namePanel
        location: class project2
        public namePanel name; // to reference the name panel with the name text field
        ^
        project2.java:1 9: cannot find symbol
        symbol : class testPanel
        location: class project2
        public testPanel test; // to reference the test panel with the 3 test grade fields
        ^
        project2.java:2 0: cannot find symbol
        symbol : class resultPanel
        location: class project2
        public resultPanel result; // to reference the result panel with the result text field
        ^
        project2.java:2 7: cannot find symbol
        symbol : class openFile
        location: class project2
        public openFile open; // to reference the open file class
        ^
        project2.java:4 0: cannot find symbol
        symbol : class resultPanel
        location: class project2
        result = new resultPanel();
        ^
        project2.java:4 2: cannot find symbol
        symbol : class gradeTypeLabel
        location: class project2
        gradeType = new gradeTypeLabel( );
        ^
        project2.java:4 4: cannot find symbol
        symbol : class namePanel
        location: class project2
        name = new namePanel();
        ^
        project2.java:4 6: cannot find symbol
        symbol : class testPanel
        location: class project2
        test = new testPanel();
        ^
        project2.java:7 6: cannot find symbol
        symbol : class CalcButtonListe ner
        location: class project2
        calcButton.addA ctionListener(n ew CalcButtonListe ner());// calls the ActionListener for the calcButton
        ^
        project2.java:7 7: cannot find symbol
        symbol : class PrevButtonListe ner
        location: class project2
        prevButton.addA ctionListener(n ew PrevButtonListe ner());// calls the ActionListener for the prevButto
        ^
        project2.java:7 8: cannot find symbol
        symbol : class NextButtonListe ner
        location: class project2
        nextButton.addA ctionListener(n ew NextButtonListe ner());// calls the ActionListener for the nextButton
        ^
        project2.java:7 9: cannot find symbol
        symbol : class SaveButtonListe ner
        location: class project2
        saveButton.addA ctionListener(n ew SaveButtonListe ner());// calls the ActionListener for the addButton
        ^
        22 errors

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          Let's start with the first error.

          Where is gradeTypePanel even defined? It looks like one of your classes, and I don't see it.

          It's referenced in your code, but there's no import for it.

          Also, in main(), you call out project2. I think it should be project3.

          Comment

          • blknmld69
            New Member
            • Feb 2010
            • 69

            #6
            Good morning, Having all kinds of compilation errors with my program. Started a new one and here it is and the errors.

            Code:
               import java.util.*; 
               import java.io.*; 
               import javax.swing.*; 
               import java.awt.*; 
               import java.awt.event.*; 
            	
               public class SuperProject extends JFrame 
               { 
                  private static final int WIDTH = 550; 
                  private static final int HEIGHT = 430; 
                  int gradeCalc; 
                  int Student; 
                  private StudentList[] sList = new StudentList[20]; 
                  private static final int MAX_NUMBER_OF_STUDENTS = 20; 
            		
               // instance variables 
                  private int noOfStudents; 
                  private double score, tst1, tst2, tst3; 
                  private double classAvg, stAvg, totalScore; 
                  private int displayedStudentIndex = 0; 
                  private char ltrGrade; 
                  private String stName; 
              
               // This area is for the GUI components 
               // Each item that will be displayed will 
               // have a label and a textfield, (L) and (TF), 
               // respectively. 
                  private JLabel stNameL, tst1L, tst2L, tst3L, 
                  classAvgL, stAvgL, headingL; 
                  private JTextField stNameTF, tst1TF, tst2TF, tst3TF; 
                  private JTextArea classAvgTA, stAvgTA; 
                  private JButton exitB, nextB, prevB, calcGrade; 
                  private ButtonHandler bHandler; 
              
                  public SuperProject() 
                  { 
                     setTitle("Grade Calculator"); 	  // set's the title 
                     setSize(WIDTH, HEIGHT); 			  // set the window size 
                     Container pane = getContentPane(); // get the container 
                     pane.setLayout(null); 				  // set the container's layout to null 
                     bHandler = new ButtonHandler();    // instantiate the button event handler 
              
                  // instantiate the labels 
                     headingL = new JLabel("STUDENT RECORD"); 
                     stNameL = new JLabel("Student Name", SwingConstants.RIGHT); 
                     tst1L = new JLabel("Test 1", SwingConstants.LEFT); 
                     tst2L = new JLabel("Test 2", SwingConstants.LEFT); 
                     tst3L = new JLabel("Test 3", SwingConstants.LEFT); 
                     stAvgL = new JLabel("Student Average " 
                        + "\n" + "Class Average"); 
            				
                  //instantiate the text fields 
                     stNameTF = new JTextField(65); 
                     tst1TF = new JTextField(10); 
                     tst2TF = new JTextField(10); 
                     tst3TF = new JTextField(10); 
            			
                  // instantiate the text area 
                     classAvgTA = new JTextArea(6, 20); 
                     classAvgTA.setAutoscrolls(true); 
                  // instantiate the buttons and register the listener 
                     exitB = new JButton("Exit"); 
                     exitB.addActionListener(bHandler); 
                     nextB = new JButton("Next"); 
                     nextB.addActionListener(bHandler); 
                     prevB = new JButton("Previous"); 
                     prevB.addActionListener(bHandler); 
                     calcGrade = new JButton("Calc Grade"); 
                     calcGrade.addActionListener(bHandler); 
                  // set the size of the labels, text fields, and buttons 
                     headingL.setSize(200, 30); 
                     stNameL.setSize(100, 30); 
                     stNameTF.setSize(100, 30); 
                     tst1L.setSize(100, 30); 
                     tst1TF.setSize(100, 30); 
                     tst2L.setSize(120, 30); 
                     tst2TF.setSize(100, 30); 
                     tst3L.setSize(100, 30); 
                     tst3TF.setSize(100, 30); 
                     classAvgTA.setSize(370, 120); 
                     calcGrade.setSize(100, 30); 
                     prevB.setSize(100, 30); 
                     nextB.setSize(100, 30); 
                     exitB.setSize(100, 30); 
            			
                  //set the location of the labels, text fields, 
                  //and buttons 
                     headingL.setLocation(220, 10); 
                     stNameL.setLocation(20, 50); 
                     stNameTF.setLocation(120, 50); 
                     tst1L.setLocation(20, 100); 
                     tst1TF.setLocation(120, 100); 
                     tst2L.setLocation(300, 50); 
                     tst2TF.setLocation(420, 50); 
                     tst3L.setLocation(300, 100); 
                     tst3TF.setLocation(420, 100); 
                     classAvgTA.setLocation(70, 230); 
                     prevB.setLocation(120, 370); 
                     exitB.setLocation(220, 370); 
                     nextB.setLocation(320, 370); 
                     calcGrade.setLocation(420, 370);
            			 
                  //add the labels, text fields, and buttons to the pane 
                     pane.add(headingL); 
                     pane.add(stNameL); 
                     pane.add(stNameTF); 
                     pane.add(tst1L); 
                     pane.add(tst1TF); 
                     pane.add(tst2L); 
                     pane.add(tst2TF); 
                     pane.add(tst3L); 
                     pane.add(classAvgTA); 
                     pane.add(calcGrade); 
                     pane.add(prevB); 
                     pane.add(exitB); 
                     pane.add(nextB); 
                     setVisible(true); //show the window 
                     setDefaultCloseOperation(EXIT_ON_CLOSE); 
                     System.exit(0); 
                  } 
            		
            		 private class ButtonHandler implements ActionListener 
                  { 
            	          public void actionPerformed (ActionEvent e)  
            	         {  
            	            if (e.getActionCommand().equals("Previous"))  
            	               if (displayedStudentIndex > 0)  
            	                  displayGradeAverage(displayedStudentIndex - 1);  
            	               else  
            	                  displayGradeAverage(displayedStudentIndex);  
            	            else if (e.getActionCommand().equals("Next"))  
            	               if (displayedStudentIndex + 1 < noOfStudents)  
            	                  displayGradeAverage(displayedStudentIndex + 1);  
            	               else  
            	                  displayGradeAverage(displayedStudentIndex);  
            	            else if (e.getActionCommand().equals("Calc Grade"))  
            	               displayGradeAverage(0);  
            	            else  
            	               System.exit(0);  
            	         }  
            			
                  public static void main (String [] args) 
                  { 
                     new SuperProject(); 
                  } 
            		
                  Scanner inFile = 
                  new Scanner(new FileReader("AcademicGrades.txt")); 
                  { 
                     for (int s = 0; s < MAX_NUMBER_OF_STUDENTS; s++) 
                        gradeCalc.StudentList[s] = new Student(); 
                     gradeCalc.noOfStudents = 
                        inFile.nextInt(); 	// get the number of students 
                     gradeCalc.score = 
                        inFile.nextDouble(); // get the student's scores 
                     gradeCalc.getStudentData(inFile); 
                     gradeCalc.displayGradeAverage(0); 
                  } 
            		
               // get the student data from file 
                  public void getStudentData(Scanner inFile) 
                  { 
                     System.out.println("Grade Calculator is getting information..."); 
                     System.out.println("One Moment Please"); 
                  } 
                      
            
            	       public void displayGradeAverage(int stName)  
            	      {  
            	         displayedStudentIndex = stName;  
            	         String strName = "";  
            	         boolean classAvg = studentList[(int) (totalScore / noOfStudents)].getClassAverage();  
            	         stNameTF.setText(StudentList[stName].getFirstName() + " "  
            	                                                            + studentList[stName].getLastName()); 
            	         stAvgTA.setText(""+StudentList[(int) (totalScore / 3.0)].getStudentAverage()); 
            	         classAvgTA.setText(""+StudentList[(int) (totalScore / noOfStudents)].getClassAverage()); } 
            	   	} 
            		}
            Then it goes to StudentList.jav a and gives the following:
            ERRORS
            ----jGRASP exec: javac -g SuperProject.ja va

            StudentList.jav a:5: class GradeCalculator 3 is public, should be declared in a file named GradeCalculator 3.java
            public class GradeCalculator 3 extends JFrame
            ^
            SuperProject.ja va:13: cannot access StudentList
            bad class file: .\StudentList.j ava
            file does not contain class StudentList
            Please remove or make sure it appears in the correct subdirectory of the classpath.
            private StudentList[] sList = new StudentList[20];
            ^

            ----jGRASP wedge2: exit code for process is 1.
            ----jGRASP: operation complete.

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              Sorry, just got in.

              First, the error "StudentList.ja va:5..." is about your "StudentList.ja va" class failing to compile. Since its failing compile, the error at line 13 just continues the problem.

              So. First thing you need to do is clean up StudentList.jav a. It looks like the main class there is actually declared as "GradeCalculato r3". You need to fix that, and any other errors that might result.

              Comment

              • blknmld69
                New Member
                • Feb 2010
                • 69

                #8
                I will try that Orally but I had to turn the project in this morning! I really do want to get it working. I appreciate all your help and I definitely will be back to the site when my Last class start in Oct.

                Once again Thank!

                Comment

                • Oralloy
                  Recognized Expert Contributor
                  • Jun 2010
                  • 988

                  #9
                  @blknmld69,

                  No worries! Good luck with your schooling.

                  Comment

                  Working...