I'm having an issue with my code and after hours of time I can't figure out what I'm doing wrong. There are three classes (GUI, Judging, TestGUI). All programs compile correctly with no syntax errors. The GUI creates an interface that should compute (JButton) a total of 8 scores and subtract the min and max, and also should reset (JButton) all the JTextFields when pressed. When running the program and pressing either the reset or compute JButtons I'm getting several errors and it is not functional. Please help me with my code.

import javax.swing.;
import java.awt.;
import java.awt.event.*;


public class GUI extends JFrame implements ActionListener

{
private final int FRAME_WIDTH = 775;
private final int FRAME_HEIGHT = 300;
private final int X_ORIGIN = 100;
private final int Y_ORIGIN = 400;


private JTextField [] judges;
private JLabel [] labels;
JButton btnReset, btnCompute;
private String coName;
private JTextField contestant, scr, s1,s2,s3,s4,s5,s6,s7,s8,s9;;
private JLabel label;
double score_a, scores[], sc1;

public GUI()
{
super("Judging");
setLayout(new FlowLayout());
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setLocation(X_ORIGIN,Y_ORIGIN);




label = new JLabel("Name of Contestant", JLabel.CENTER);
contestant = new JTextField(18);
contestant.setEditable(true);
contestant.setBackground(Color.white);
add(label);
add(contestant);

JTextField [] judges = new JTextField[8];
JLabel [] labels = new JLabel[8];

for(int i = 0;i < judges.length;i++)
{


labels[i] = new JLabel("judge" +(i+1), JLabel.LEFT);
judges[i] = new JTextField(3);

labels[i].setVerticalAlignment(JLabel.TOP);

judges[i].setBackground(Color.white);
judges[i].setForeground(Color.black);
judges[i].addActionListener(this);
judges[i].setEditable(true);
add(labels[i]);
add(judges[i]);


}

btnCompute = new JButton("Compute Score");
btnCompute.setBackground(Color.black);
btnCompute.setForeground(Color.white);
btnCompute.addActionListener(this);
add(btnCompute);

label = new JLabel("Contestant's Score", JLabel.LEFT);
label.setVerticalAlignment(JLabel.TOP);
scr = new JTextField(10);
scr.setEditable(false);
scr.setBackground(Color.white);
add(scr);
add(label);

label = new JLabel("Reset Scores for Next Contestant", JLabel.LEFT);
btnReset = new JButton("Reset");
btnReset.setBackground(Color.gray);
btnReset.setForeground(Color.red);
btnReset.addActionListener(this);

add(btnReset);

}

public void setScores(double [] scores)
{
this.score_a = score_a;
}
public double [] getScores()
{
return scores;
}

public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == btnCompute)
{
s1.setEditable(true);
s2.setEditable(true);
s3.setEditable(true);
s4.setEditable(true);
s5.setEditable(true);
s6.setEditable(true);
s7.setEditable(true);
s8.setEditable(true);


}


else if (source == btnReset)
{
s1.setText("");
s1.setEditable(true);
s2.setText("");
s3.setText("");
s4.setText("");
s5.setText("");
s6.setText("");
s7.setText("");
s8.setText("");
contestant.setText("");
s1.setEditable(true);
s2.setEditable(true);
s3.setEditable(true);
s4.setEditable(true);
s5.setEditable(true);
s6.setEditable(true);
s7.setEditable(true);
s8.setEditable(true);
contestant.setEditable(true);

}
}
}

The errors below are a few of the several lines I receive when pressing the compute JButton. I'm thinking it may be because a variable is declared wrong or I haven't called a class correctly but I can't find my error.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUI.actionPerformed(GUI.java:104)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

The error rised when you try to call a methos setEditable on the JTextField s1; because you it is not initialized yet;
Try to initialize it as you do with contestant and scr using:

s1=new JTextField();

you have to do the same thing with all the textfields JTextField contestant, scr, s1,s2,s3,s4,s5,s6,s7,s8,s9

Hope it helps.

and change this

public void setScores(double [] scores)
{
this.score_a = score_a;
}

To

public void setScores(double [] scores)
{
this.score_a = scores;
}

Sorry this is not true;

But maybe you need this

public void setScores(double [] scores)
{
this.scores = scores;
}

The error rised when you try to call a methos setEditable on the JTextField s1; because you it is not initialized yet;
Try to initialize it as you do with contestant and scr using:

s1=new JTextField();

you have to do the same thing with all the textfields JTextField contestant, scr, s1,s2,s3,s4,s5,s6,s7,s8,s9

Hope it helps.

Okay. I see what you mean but I initialized the JTextFields in the GUI with the Judges[] array, contestant, and scr. The ActionPerformed class is supposed to take the output from those TextFields and do as I described above (compute, reset). So is it necessary for me to initialize s1-s9 and create a new textfield for s1 or is what I'm doing altogether incorrect? Wasn't sure what to do with the array judges[] or how to call another class in the ActionPerformed. Just a bit confused.

if you want do this with array you should do somthing like:

public class GUI extends JFrame implements ActionListener
{
//Declarations here

private JTextField [] judges=new JTextField[8];

//the rest of your declaration here

//the default coonstructor:
public GUI()
{
// some instructions as you done

//do not put that here
//JTextField [] judges = new JTextField[8];

//the reste of your code inside
}

//and in the action listener you should refer to the fields text by this
judges[0].setEditable(true);
//.....
judges[7].setEditable(true);
}

Hope ir helps.

First post
Line 19

private JTextField contestant, scr, s1, s2, s3, s4, s5, s6, s7, s8, s9;

fields s1, s2, s3, s4, s5, s6, s7, s8, s9 are null. Where are new, new, new...... instances of them?

Using the array you code should look like this:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUI extends JFrame implements ActionListener {

    private final int FRAME_WIDTH = 775;
    private final int FRAME_HEIGHT = 300;
    private final int X_ORIGIN = 100;
    private final int Y_ORIGIN = 400;
    private JTextField[] judges = new JTextField[8];
    private JLabel[] labels;
    JButton btnReset, btnCompute;
    private String coName;
    private JTextField contestant, scr;// s1, s2, s3, s4, s5, s6, s7, s8, s9;

    ;
    private JLabel label;
    double score_a, scores[], sc1;

    public GUI() {
        super("Judging");
        setLayout(new FlowLayout());
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setLocation(X_ORIGIN, Y_ORIGIN);
        label = new JLabel("Name of Contestant", JLabel.CENTER);
        contestant = new JTextField(18);
        contestant.setEditable(true);
        contestant.setBackground(Color.white);
        add(label);
        add(contestant);

//JTextField [] judges = new JTextField[8];
        JLabel[] labels = new JLabel[8];

        for (int i = 0; i < judges.length; i++) {


            labels[i] = new JLabel("judge" + (i + 1), JLabel.LEFT);
            judges[i] = new JTextField(3);

            labels[i].setVerticalAlignment(JLabel.TOP);

            judges[i].setBackground(Color.white);
            judges[i].setForeground(Color.black);
            judges[i].addActionListener(this);
            judges[i].setEditable(true);
            add(labels[i]);
            add(judges[i]);


        }

        btnCompute = new JButton("Compute Score");
        btnCompute.setBackground(Color.black);
        btnCompute.setForeground(Color.white);
        btnCompute.addActionListener(this);
        add(btnCompute);

        label = new JLabel("Contestant's Score", JLabel.LEFT);
        label.setVerticalAlignment(JLabel.TOP);
        scr = new JTextField(10);
        scr.setEditable(false);
        scr.setBackground(Color.white);
        add(scr);
        add(label);

        label = new JLabel("Reset Scores for Next Contestant", JLabel.LEFT);
        btnReset = new JButton("Reset");
        btnReset.setBackground(Color.gray);
        btnReset.setForeground(Color.red);
        btnReset.addActionListener(this);

        add(btnReset);

    }

    public void setScores(double[] scores) {
        this.scores = scores;
    }

    public double[] getScores() {
        return scores;
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == btnCompute) {
            for (int i = 0; i < judges.length; ++i) {
                judges[i].setEditable(true);
            }

        } else if (source == btnReset) {
            for (int i = 0; i < judges.length; ++i) {
                judges[i].setText("");
            }
            contestant.setText("");
            for (int i = 0; i < judges.length; ++i) {
                judges[i].setEditable(true);
            }
            contestant.setEditable(true);
        }
    }
//I added the main method for test
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
}

hope it helps.

if you want do this with array you should do somthing like:

public class GUI extends JFrame implements ActionListener
{
//Declarations here

private JTextField [] judges=new JTextField[8];

//the rest of your declaration here

//the default coonstructor:
public GUI()
{
// some instructions as you done

//do not put that here
//JTextField [] judges = new JTextField[8];

//the reste of your code inside
}

//and in the action listener you should refer to the fields text by this
judges[0].setEditable(true);
//.....
judges[7].setEditable(true);
}

Hope ir helps.

That helped tremendously. Reset button clears textfields but still errors out, and doesn't clear contestant textfield. Compute button doesn't error out but also is not functional. I have a seperate class called judging and I have been having trouble calling the calculateScores() function. to make the compute button function. Any advicce?

public  void calculateScore()
       {
        int largest, smallest;
      
        
         if (n > scores.length)
        {
            System.out.println("The size of the array does not equal the "+
                "scores asked for");
            System.exit(0);
        }
        
        if (n < 3)
        {
            System.out.println("There must be at least 3 scores");
            return;
        }
        
        largest = findMax();
        smallest = findMin();
        
        total = 0;
        
        for(int i = 0; i < n; ++i)
                total += scores[i];
                
        total -= (scores[smallest] + scores[largest]);        
        
       
    }

the information sent is not enough. I could not figure out what logic is implementd in the judging class.

the information sent is not enough. I could not figure out what logic is implementd in the judging class.

Sorry.

import java.util.*;

public class Judging
{

    private double [] scores;
    private double total;
    private int n;
    private String contestant;

    public Judging(int n, String contestant)
    {
       
        this.n = n;
        this.contestant = contestant;
        scores = new double[n];
        total = 0;
        
    }    
    public void getData(Scanner console)
    {   
        boolean ok;
        
        if (n > scores.length)
        {
            System.out.println("The size of the array does not equal the "+
                "scores asked for");
            System.exit(0);
        }        
        
            for(int i=0;i<n;i++)
            {
                do
                {
                    System.out.print("Enter the score for the judge number "+(i+1)+": ");
                        scores [i]= console.nextDouble();           
                
                    if (scores[i] < 1 || scores[i] > 10)
                    System.out.println("Sorry!  The score must be between 1 and 10");
          
            }
                while (scores[i] < 1 || scores[i] > 10);
            }
                console.nextLine();             
              
     }

       public  void calculateScore()
       {
        int largest, smallest;
      
        
         if (n > scores.length)
        {
            System.out.println("The size of the array does not equal the "+
                "scores asked for");
            System.exit(0);
        }
        
        if (n < 3)
        {
            System.out.println("There must be at least 3 scores");
            return;
        }
        
        largest = findMax();
        smallest = findMin();
        
        total = 0;
        
        for(int i = 0; i < n; ++i)
                total += scores[i];
                
        total -= (scores[smallest] + scores[largest]);        
        
       
    }

    public double getTotal()
    {
        return total;
    }
    
    public double [] getScores()
    {
        return scores;
    }
    
    public String getContestant()
    {
        return contestant;
    }
    
    public void setContestant(String newContestant)
    {
        contestant = newContestant;
    }
    
    public void setScores(double [] scores)
    {
        this.scores = scores;
    }
    
    
     private  int findMin()
      {
          
          
         if (n <= 0 || n > scores.length)
         {
               System.out.println("You have sent the wrong"+
                      " value for the number of elements in the array for"+
                      " findMin. -1 is being returned");
               return - 1;
          }
          if (n < 0 || 0 > scores.length)
          {
                 System.out.println("You have sent the "+
                      "wrong value for the starting element to search in "+
                      "the array for findMin.  -1 is being returned");
                return - 1;
          }
          int minIndex = 0;
          double minValue = scores[0];
          
          for(int i = 0 +1; i < n; ++i)
                if (scores[i] < minValue)
                {
                    minValue = scores[i];
                    minIndex = i;
                }
                
          return minIndex;      
     }
     
     private  int findMax()
     {
          if (n <= 0 || n > scores.length)
         {
               System.out.println("You have sent the wrong"+
                      " value for the number of elements in the array for"+
                      " findMax. -1 is being returned");
               return - 1;
          }
          if (0 < 0 || 0 > scores.length)
          {
                 System.out.println("You have sent the "+
                       "wrong value for the starting element to search in "+
                       "the array for findMax.  -1 is being returned");
                 return - 1;
          }
          
          int maxIndex = n-1;
          double maxValue = scores[n-1];
          
          for(int i = n-1; i > 0; --i)
                if (scores[i] > maxValue)
                {
                    maxValue = scores[i];
                    maxIndex = i;
                }
                
          return maxIndex;      
     }
}

Can you gave more information about those data members, please:

private double [] scores;
    private double total;
    private int n;
    private String contestant;

Can you gave more information about those data members, please:

private double [] scores;
    private double total;
    private int n;
    private String contestant;

private double [] scores; // array of scores is set to double to handle input for Judges 1-8 scores.
private double total;//total is the total of the array of scores minus the max and min
private int n;
private String contestant;//contestant is declared to assign the contestants name to a String.

The main method is in my testGui class-

public class TestGUI
{
    public static void main(String [] args)
   {
            GUI example = new GUI();
            example.setVisible(true);
    }

I'm trying to call the method in the GUI class that computes all the numbers (subtracting max and min), and tests for errors from the judging class. Tried double score = calculateScore();- couldn't find calculateScore method.

but calculateScore is declard as vois in the Judging class; you should modify it to double an return a value. Her it is asumed that there is an onstance of the calsse Judging that we call the function on it.

but calculateScore is declard as vois in the Judging class; you should modify it to double an return a value. Her it is asumed that there is an onstance of the calsse Judging that we call the function on it.

Ah yes... I kept switching things around and missed that. Okay lets say I return the value total and declare the Judging class a double. How do I call that value in the ActionPerformed method to compute the scores correctly?

I assumed that you have in the Judging some declaratoin like:

public  double calculateScore(){

return SomedoubleValue;
}

Then in the GUI calss you should create an instance of Judging calss: some thing like:

Judging judging=nwe Judging(yourIntNumberHere, "contestant");

and then call the function calculateScore like this:

double result=judging.calculateScore();

and then you can use the result variable as you wish.


Hope it helps.

I have made the changes as suggested but I'm still not getting the compute button to function correctly. Not sure what I'm doing wrong.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class GUI extends JFrame implements ActionListener
{
    
     
     private final int FRAME_WIDTH = 775;
     private final int FRAME_HEIGHT = 300;
     private final int X_ORIGIN = 100;
     private final int Y_ORIGIN = 400;
    
     
    private JTextField [] judges=new JTextField[8];
    private JLabel [] labels;
    JButton btnReset, btnCompute;
    private String coName;
    private JTextField contestant, scr;
    private JLabel label;
    double score_a, scores[], sc1;
    Judging judging=new Judging(8, "scores");
        
    
   public GUI()
   {
        super("Judging");
        setLayout(new FlowLayout());
        setSize(FRAME_WIDTH,FRAME_HEIGHT);
        setLocation(X_ORIGIN,Y_ORIGIN);
        
        

        
        label = new JLabel("Name of Contestant", JLabel.CENTER); 
        contestant = new JTextField(18);
        contestant.setEditable(true);
        contestant.setBackground(Color.white);
        add(label);
        add(contestant);
               
        JLabel [] labels = new JLabel[8];
                  
        for(int i = 0;i < judges.length;i++)
        {
            

            labels[i] = new JLabel("judge" +(i+1), JLabel.LEFT);
            judges[i] = new JTextField(3);
 
            labels[i].setVerticalAlignment(JLabel.TOP); 
           
            judges[i].setBackground(Color.white);
            judges[i].setForeground(Color.black);
            judges[i].addActionListener(this);
            judges[i].setEditable(true);
            add(labels[i]);
            add(judges[i]);
             
                       
        }
        
           btnCompute = new JButton("Compute Score");
           btnCompute.setBackground(Color.black);
           btnCompute.setForeground(Color.white);
           btnCompute.addActionListener(this);
           add(btnCompute);
           
           label = new JLabel("Contestant's Score", JLabel.LEFT); 
           label.setVerticalAlignment(JLabel.TOP); 
           scr = new JTextField(10);
           scr.setEditable(false);
           scr.setBackground(Color.white);
           add(scr);
           add(label);
           
           label = new JLabel("Reset Scores for Next Contestant", JLabel.LEFT);
           btnReset = new JButton("Reset");
           btnReset.setBackground(Color.gray);
           btnReset.setForeground(Color.red);
           btnReset.addActionListener(this);
           add(btnReset);
       
    }

    public void setScores(double [] scores) 
    {
            this.scores = scores;
    }
    public double [] getScores()
    {
            return scores;
    }

   
    public void actionPerformed(ActionEvent e) 
    {
        

        Object source = e.getSource();
        double total= judging.calculateScore();
           
            if (source == btnCompute) 
            {
                
                for (int i = 0; i < judges.length; ++i) 
                {
                    judges[i].setEditable(true);
                }
            } 
            else if (source == btnReset) 
            {
                for (int i = 0; i < judges.length; ++i) 
                {
                    judges[i].setText("");
                }
                    contestant.setText("");

                for (int i = 0; i < judges.length; ++i) 
                {
                    judges[i].setEditable(true);
                }
                    contestant.setEditable(true);
        
             }
 
    }   

}

Hi;
Let see the findMax function first:

private int findMax() {
[B]//Look here n>scores.length
//n is allways egale à scores.length as you have in the constructor
//those instructions:
//  this.n = n;
//     scores = new double[n];[/B]
        if (n <= 0 || n > scores.length) {
            System.out.println("You have sent the wrong"
                    + " value for the number of elements in the array for"
                    + " findMax. -1 is being returned");
            return - 1;
        }
[B]//Look here 0<0[/B]
//0 could never be less the 0;

        if (0 < 0 || 0 > scores.length) {
            System.out.println("You have sent the "
                    + "wrong value for the starting element to search in "
                    + "the array for findMax.  -1 is being returned");
            return - 1;
        }

        int maxIndex = n - 1;
        double maxValue = scores[n - 1];

        for (int i = n - 1; i > 0; --i) {
            if (scores[i] > maxValue) {
                maxValue = scores[i];
                maxIndex = i;
            }
        }

        return maxIndex;
    }

But the most importante think you did not acte the values entred in the textFilds that means the score array in the judging class is not set:
you shoud do some thing lik that in the action performed:

if (source == btnCompute) {

            for (int i = 0; i < judges.length; ++i) {
                judges[i].setEditable(true);
                //Here please trow some exception
                scores = judging.getScores();
                scores[i] = Double.parseDouble(judges[i].getText());
            }
            judging.setScores(scores);
            double total = judging.calculateScore();
//put the total in you textField
//Here for test
            System.out.println(total);
        }
//else...

This will give you the computation of the score
but there is mutch more observation in the code.
Hope it helps.

//n is allways egale à scores.length as you have in the constructor...

egale a? I don't quite understand why this.n = n, just understanding it needed to be in the constructor as well as scores being equal to a double array of n.

If we have yhis:

public Judging(int n, String contestant) {
        this.contestant = contestant;
        this.n = n;
        scores = new double[n];
        total = 0;
    }

Then the

scores.length;

is equal to n
isn it?
if you'r not agree try this test in your main method:

int n=5;
        double t[]=new double[n];
        System.out.println(t.length);

this will print 5

I mean egale to not à sorry.

Look at this and you will understand.

int n=8;
double[] array=new double[n];
System.out.println(array.lenght);

this will print 8;
and this is your case.
hope it helps.

Thank you for your help. I really appreciate 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.