简体   繁体   中英

How can I get this ActionListener to work?

I'm having a problem with an ActionListener for a coding challenge in my Java class. It's a car simulator on a GUI. The accelerate button should add 8 miles to the car, while the Brake button (haven't written that yet) should decrease six miles. The problem is, if I run the program and click the accelerate button, the terminal will deliver an error me.

The car class:

import javax.swing.*;

public class Car 
{
private int yearModel;
private String make;
private double speed;

public Car(int newModel, String newMake)
{
   speed = 0;
   make = newMake;
   yearModel = newModel;
}

public void accelerate()
{
   //increase the speed by 8 when the accelerate method is called
   speed += 8;
}

public void brake()
{
   //decrease the speed by 7 when the accelerate method is called
   speed -= 6;
}

public String getMake()
{
   return make;
}

public int getModel()
{
   return yearModel;
}

public double getSpeed()
{
   return speed;
}

}

The CarView class.

import javax.swing.*;
import java.awt.event.*;
public class CarView extends JFrame 
{
//CarView has a accelerate button
private JButton accelerate;
//CarView has-a brake button
private JButton brake;
//CarView has-a Label for the make
private JLabel make;
private JTextField makeInput;
private JTextField modelYearInput;
private JTextField speedInput;
private JLabel speed;
private Car car;

public CarView(Car newCar)
{
    this.car = newCar;
    setTitle("CarView Class");
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
    JPanel contents = new JPanel();
  
    JButton accelerate = new JButton("Accelerate");
    accelerate.addActionListener(new AccelerateButtonListener(newCar, speedInput));
  
    JButton brake = new JButton("brake");
  
    JTextField modelYearInput = new JTextField(Integer.toString(newCar.getModel()));
    JTextField speedInput = new JTextField(Double.toString(newCar.getSpeed()));
  
    modelYearInput.setColumns(10);
  
    contents.add(new JLabel("make"));
    contents.add(new JTextField(newCar.getMake()));

    makeInput = new JTextField(newCar.getMake());
    makeInput.setColumns(10);
  
    contents.add(new JLabel("Car Model"));
    contents.add(modelYearInput);
  
    contents.add(new JLabel("Speed"));
    contents.add(speedInput);
  
    contents.add(brake);
    contents.add(accelerate);

    add(contents);
  
    setVisible(true);
    }

    public void showSpeed()
    {
        speedInput.setText("" + car.getSpeed());
    }
 
    public static void main()
    {
        new CarView(new Car(1996, "BMW"));
    }

private class AccelerateButtonListener implements ActionListener
{
   private JTextField speedInputField;
   private Car car;
   public AccelerateButtonListener(Car newCar, JTextField newSpeedInput)
   {
       speedInputField = newSpeedInput;
       car = newCar;
   }
   
   public void actionPerformed(ActionEvent e)
   {
       car.accelerate();
       speedInput.setText(Double.toString(car.getSpeed()));
   }
}

The following line of your code is throwing NullPointerException .

speedInput.setText(Double.toString(car.getSpeed()));

That line is in method actionPerformed() of class AccelerateButtonListener .

The cause is this line of your code (in the constructor of class CarView ):

JTextField speedInput = new JTextField(Double.toString(newCar.getSpeed()));

You are creating a local variable with the same name as the class member variable. As a result, the class member is null. You need to remove the JTextField , ie that line should be:

speedInput = new JTextField(Double.toString(newCar.getSpeed()));

I guess you need to do the same with the line that precedes that line, ie change this line of your code

JTextField modelYearInput = new JTextField(Integer.toString(newCar.getModel()));

to this:

modelYearInput = new JTextField(Integer.toString(newCar.getModel()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM