Java 当我试图运行我的第三个类(墙纸)时,我在第二个类中得到了NullPointerException

Java 当我试图运行我的第三个类(墙纸)时,我在第二个类中得到了NullPointerException,java,swing,Java,Swing,不管我改变了什么变量,我总是会犯这样的错误,我想知道我是否会把事情弄得一团糟 这是我的主课 import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class menu implements ActionListener { /*pu

不管我改变了什么变量,我总是会犯这样的错误,我想知道我是否会把事情弄得一团糟

这是我的主课

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class menu implements ActionListener {

    /*public static JCheckBox check;
    public static JCheckBox check1;*/
    public static JPanel panel;
    public static JFrame frame;
    public static JButton Wallpaper;
    public static JButton tile;
    public static JButton trim;



    public static void main(String[] args) {

        panel = new JPanel();

        frame = new JFrame();
        frame.setSize(350, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(555,318);
        frame.add(panel);

        Border dash = BorderFactory.createLineBorder(Color.BLACK);

        //create the MainMenu title
        JLabel MainMenu = new JLabel("  MainMenu");
        MainMenu.setBounds(30, 10, 80, 25);
        MainMenu.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black));
        MainMenu.setForeground(Color.black);
        panel.add(MainMenu);

        //creates the wallpaper button
        Wallpaper = new JButton("Wallpaper");
        Wallpaper.setBounds(20,40,100,25);
        Wallpaper.addActionListener(new menu());
        panel.add(Wallpaper);

        //creates the tile button
        tile = new JButton("Tile");
        tile.setBounds(20,70,100,25);
        panel.add(tile);

        //creates the trim button
        trim = new JButton("Trim");
        trim.setBounds(20,100,100,25);
        trim.addActionListener(new menu());
        panel.add(trim);

        panel.setLayout(null);
        frame.setVisible(true);

        //i was playing around do not mind that :)

     /*   check = new JCheckBox();
        check.setBounds(130, 30, 100, 25);
        panel.add(check);

        check1 = new JCheckBox();
        check1.setBounds(130, 50, 100, 25);
        panel.add(check1);

        check2 = new JCheckBox();
        check2.setBounds(130, 70, 100, 25);
        panel.add(check2);
      */

    }

   @Override
    public void actionPerformed(ActionEvent e) {
        //makes the Trim button actully do something
       if (e.getSource() == Wallpaper){
           WallpaperCalculator.Wallpaper();
       }
       if (e.getSource() == trim) {
            TrimCalculator.Trim();
        }
    }
}
这是我的第二节课,我一直在第90行得到错误

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

public class TrimCalculator implements ActionListener {
    private static JTextField lengthText;
    private static JTextField widthText;
    private static JButton button;
    private static JLabel total;
    private static JLabel walls;
    private static JTextField wallsText;


    public static void Trim() {

        //creates the gui it self
        JPanel panel = new JPanel();
        panel.setLayout(null);
        JFrame frame = new JFrame();
        frame.setSize(350,300);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocation(555,318);
        frame.add(panel);

        //sets the title
        JLabel title = new JLabel();
        title.setBounds(10, 20, 300, 25);
        title.setText("Trim Calculator");
        panel.add(title);

        //sets the lengths title
        JLabel length = new JLabel();
        length.setBounds(10,55,300,25);
        length.setText("Length Input");
        panel.add(length);

        //creates a field for the length text input
        lengthText = new JTextField();
        lengthText.setBounds(120,55,90,25);
        panel.add(lengthText);

        //creates a width text label
        JLabel width = new JLabel();
        width.setBounds(10,80,300,25);
        width.setText("Width Input");
        panel.add(width);

        //created a field for the width text input
        widthText = new JTextField();
        widthText.setBounds(120,80,90,25);
        panel.add(widthText);


        //makes a field to set a text box so people can see where the total will be
        JLabel totalAmount = new JLabel();
        totalAmount.setBounds(10,150,300,25);
        totalAmount.setText("Total Amount:");
        panel.add(totalAmount);

        //makes a field to present the answer
        total = new JLabel();
        total.setBounds(125, 150, 90,25);
        panel.add(total);

        walls = new JLabel("Walls");
        walls.setBounds(10,105,90,25);
        panel.add(walls);

        wallsText = new JTextField();
        wallsText.setBounds(120,105,90,25);
        panel.add(wallsText);


        //makes a button to get the two lengths and start the multiplication process
        button = new JButton();
        button.setBounds(10,130,90,25);
        button.setText("Find");
        button.setForeground(Color.CYAN);
        button.addActionListener(new TrimCalculator());
        panel.add(button);

        frame.setVisible(true);

    }

    //gets the button and changes it from a string into a int so it can be calculated
    @Override
    public void actionPerformed(ActionEvent e) {
     String lengthAmount = lengthText.getText();<— line that the error is throwing me to

        String widthAmount = widthText.getText();
        String wallAmount = wallsText.getText();

        //changes the strings into a double
        double length = Double.parseDouble(lengthAmount);
        double width = Double.parseDouble(widthAmount);
        double wallz = Double.parseDouble(wallAmount);

        // adds the two sides together
        double sides = length + width;

        //multiplies sides1 by 2 to get preTotal
        double preTotal = sides * 2;

        //gets total2 and divides it by 8 to get the final total
        double Total = preTotal/8;

        //times the total by the walls given.
        double newTotal = Total * wallz;

        //changes the double variable newTotal into a string
        String f = String.valueOf(newTotal);

        //makes the button actually print the amount of trim needed
        if (e.getSource() == button){
            total.setText(f+" ft");
        }

    }
}

再次感谢您的帮助

我找到了答案,但是对于那些确实出现NullPointerException错误的人,如果你有很多类,那么请一直引用/**/一个类,如果你复制并粘贴代码,看看它可能复制到哪里。

这不是错误代码。这是引发异常的线程的名称。发布异常,包括stracktrace。Á并请指定第二个类中的第90行。String lengthAmount=lengthText.getText()这是它一直将我发送到的行,哦,我也不知道它只是这样说,并在旁边说了错误。1)变量名不应以大写字符开头。大多数是正确的,但不是全部。始终如一!2) 您的类不应该为Swing组件使用静态变量。1)请参见&2)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class WallpaperCalculator implements ActionListener {
    private static JTextField lengthText2;
    private static JTextField heightText;
    private static JButton button2;
    private static JLabel total;

    public static void Wallpaper() {

        JPanel panel = new JPanel();
        panel.setLayout(null);

        JFrame frame = new JFrame();
        frame.setSize(350,300);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocation(555,318);
        frame.add(panel);

        //sets the title
        JLabel title = new JLabel();
        title.setBounds(10, 20, 300, 25);
        title.setText("Wallpaper Calculator");
        title.setForeground(Color.BLUE);
        panel.add(title);

        //sets the lengths title
        JLabel length = new JLabel();
        length.setBounds(10,55,300,25);
        length.setText("Length Input");
        panel.add(length);

        //creates a field for the length text input
        lengthText2 = new JTextField();
        lengthText2.setBounds(120,55,90,25);
        panel.add(lengthText2);

        //creates a width text label
        JLabel height = new JLabel();
        height.setBounds(10,80,300,25);
        height.setText("height Input");
        panel.add(height);

        //created a field for the width text input
        heightText = new JTextField();
        heightText.setBounds(120,80,90,25);
        panel.add(heightText);


        //makes a field to set a text box so people can see where the total will be
        JLabel totalAmount = new JLabel();
        totalAmount.setBounds(10,150,300,25);
        totalAmount.setText("Total Amount:");
        panel.add(totalAmount);

        //makes a field to present the answer
        total = new JLabel();
        total.setBounds(125, 150, 90,25);
        panel.add(total);


        //makes a button to get the two lengths and start the multiplication process
        button2 = new JButton();
        button2.setBounds(10,130,90,25);
        button2.setText("Find");
        button2.setForeground(Color.blue);
        button2.addActionListener(new TrimCalculator());
        panel.add(button2);

        frame.setVisible(true);

    }

    //gets the button and changes it from a string into a int so it can be calculated
    @Override
    public void actionPerformed(ActionEvent c) {
        String lengthAm = lengthText2.getText();
        String heightAm = heightText.getText();

        //changes the strings into a double
        double length = Double.parseDouble(lengthAm);
        double height = Double.parseDouble(heightAm);

        // multiplies the two sides
        double sidesTotal = length * height;

        //divides the sidesTotal to get Total
        double Total = sidesTotal / 40;
        System.out.println(Total);

        //changes the double variable newTotal into a string
        String f = String.valueOf(Total);

        //makes the button actually print the amount of trim needed
        if (c.getSource() == button2){
            total.setText(f+" ft");
        }

    }

}