-3

I keep getting that error no matter what variables I change, and I was wondering If I might of messed up somewhere along my lines.

This is my main class

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();
        }
    }
}

Here is my second class where I keep getting the error on line 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");
        }

    }
}

And here is my last class that's where I click the button to try to get an answer but it send me to TrimCalculator for some reason?

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");
        }

    }

}

Thank you again for the help!

  • 1
    That is not an error code. That is the name of the thread in which an exception is thrown. Post that exception including the stracktrace. Ánd please specify which line is line 90. – f1sh Sep 09 '20 at 20:01
  • String lengthAmount = lengthText.getText() In the second class This is the line that it keeps send me to, also oh didn’t know it just said that and said error next to it. – Sniffie FTW Sep 09 '20 at 20:22
  • 1
    1) Variable names should NOT start with an upper case character. Most are correct, but not all. Be consistent! 2) Your classes should NOT use static variables for you Swing components. – camickr Sep 09 '20 at 21:00
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. They are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding & borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 10 '20 at 02:21

1 Answers1

0

I figured it out, but for those that do get the NullPointerException error, if you have many class, then quote/* */ one class all the way out and see where it might be duplicated, if you ever copy and paste code.