Transfer tekstu z JTextArea do innej klasy

0

Witam,
mam problem z przeniesieniem tekst do z JTextArea do innej klasy. Próbowałem utworzyć getter ale cały czas wyskakuję mi ten błąd.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at me.edsel.MonIng.monIngII.Window.Bank.account(Bank.java:38) at me.edsel.MonIng.monIngII.Window.Bank.start(Bank.java:28) at me.edsel.MonIng.monIngII.Window.Window$4.actionPerformed(Window.java:164) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6539) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6304) at java.awt.Container.processEvent(Container.java:2239) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2297) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476) at java.awt.Container.dispatchEventImpl(Container.java:2283) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84) at java.awt.EventQueue$4.run(EventQueue.java:733) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.awt.EventQueue.dispatchEvent(EventQueue.java:730) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

package me.edsel.MonIng.monIngII.Window;

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

public class Bank{

    JFrame f;
    JPanel panel = new JPanel();

    Window class1 = new Window();

    private static final int width = 1280;
    private static final int height = 720;

    JLabel accountBalanceText;
    JLabel accountBalance;
    JLabel possibleRelease;
    JLabel spendMoney;

    public Bank() {
    }

    public  void start() throws SQLException {
        window();
        labels();
        account();
        f.setVisible(true);
    }

    public void account() throws SQLException {
        String url =
        String username =
        String password =
        Connection con = DriverManager.getConnection(url, username, password);

        String query2 = "select name, nick, balance from bankAccout" + " WHERE name= " + "'" + class1.testText.getText().toString() + "'";
        Statement statement = con.createStatement();
        ResultSet rs = statement.executeQuery(query2);

        while (rs.next()) {
            double balance = rs.getDouble("balance");

            accountBalance = new JLabel(balance + " zł");
            accountBalance.setBounds(100, 300, 800, 400);
            accountBalance.setFont(new Font("TimesRoman", Font.BOLD, 140));
            panel.add(accountBalance);

            f.setVisible(true);
        }
        rs.close();
    }

    public void window() {
        f = new JFrame("MonIng");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(width, height);
        panel.setLayout(null);
        f.setLocationRelativeTo(null);
        f.add(panel);
    }

    public void labels() {
        accountBalanceText = new JLabel("Masz na koncie:");
        accountBalanceText.setBounds(100, 400, 310, 40);
        accountBalanceText.setFont(new Font("TimesRoman", Font.BOLD, 30));
        panel.add(accountBalanceText);
    }

}

package me.edsel.MonIng.monIngII.Window;

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

public class Window extends JFrame {

private static final int width = 1280;
private static final int height = 720;

JFrame f;
JFrame f2;
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();

JButton test;
JButton test2;
JButton test3;
JButton test4;

JLabel accountBalanceText;
JLabel accountBalance;
JLabel possibleRelease;
JLabel spendMoney;

JTextArea spendMoneyText;
JTextArea testText;

public Window() {
}

public void start() {
    Frame();
    textFields();
    buttons();
    actions();
    labels();
    setVisible(true);
}

public void Frame() {
    f = new JFrame("MonIng");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(width, height);
    panel.setLayout(null);
    setLocationRelativeTo(null);
    add(panel);

}

public void textFields() {
    spendMoneyText = new JTextArea();
    spendMoneyText.setBounds(500, 500, 200, 30);
    spendMoneyText.setFont(new Font("TimesRoman", Font.BOLD, 23));
    panel.add(spendMoneyText);

    testText = new JTextArea();
    testText.setBounds(500, 400, 200, 30);
    testText.setFont(new Font("TimesRoman", Font.BOLD, 23));
    panel.add(testText);

}

public void actions() {
    test.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            displayData();
        }
    });

    test2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!spendMoneyText.getText().isEmpty()) {
                insertInto();
                System.out.println("as");
            } else {
                JOptionPane.showMessageDialog(null, "Pole nie może byc puste!");
            }
        }
    });
    test3.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (!testText.getText().isEmpty()) {
                    getBalance(testText.getText());
                } else {
                    JOptionPane.showMessageDialog(null, "Pole nie może byc puste!");
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    });

    test4.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Bank b = new Bank();

            try {
                b.start();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    });

}
0

Tak na szybko to wyglada jakbys nie ustawial tekstu dla obiektu testText. Ustawiasz jedynie wymiary i fonta, a pozniej w kodzie operujesz na tekscie i leci NPE.

0

Chodzi o tą linie kodu

 String query2 = "select name, nick, balance from bankAccout" + " WHERE name= " + "'" + class1.testText.getText().toString() + "'";
0

No tak xd ale nie ustawiasz tego tekstu, wiec jest pewnie nullowy i sie wypierdziela jak wolasz toString()

0

Nie widzę maina więc będzie trudniej.

Pewnie nigdy nie wywołujesz Window#start(),
więc Window#textFields() się nigdy nie wywołuje,
więc testText = new JTextArea(); się nigdy nie wykonuje,
więc do testText nigdy nic nie zostanie przypasne,
więc ma domyślną wartość równą null,
więc class1.testText.getText().toString() to tak naprawdę class1.null.getText().toString(),
a że null nie ma takiej metody jak getText() to ci program się ubija

Prawdziwym powode jest niesamowity bałagan w tym kodzie. Proponuję ci to poprawić:

  • Trzy razy tworzysz new JFrame(), jeden w Bank#window, drugi w Window#Frame, a trzeci to sam Window jest JFrame, więc new Window() też ci robi new Frame(). Który z tych trzech frameów wyświetlasz?
  • W Bank tworzysz Window i... nigdy go nie używasz
0

@Tyvrel:

public class Main {

    public static void main(String[] args) throws SQLException {
        Window w = new Window();
        w.start();

    }
}
public class Bank{

    JFrame f;
    JPanel panel = new JPanel();

    Window class1 = new Window();

    private static final int width = 1280;
    private static final int height = 720;

    JLabel accountBalanceText;
    JLabel accountBalance;
    JLabel possibleRelease;
    JLabel spendMoney;

    public Bank() {
    }

    public  void start() throws SQLException {
        window();
        labels();
        account();
        f.setVisible(true);
    }

    public void account() throws SQLException {
        String url = 
        String username = 
        String password = 
        Connection con = DriverManager.getConnection(url, username, password);

        String query2 = "select name, nick, balance from bankAccout" + " WHERE name= " + "'" + "pies" + "'";
        Statement statement = con.createStatement();
        ResultSet rs = statement.executeQuery(query2);

        while (rs.next()) {
            double balance = rs.getDouble("balance");

            accountBalance = new JLabel(balance + " zł");
            accountBalance.setBounds(100, 300, 800, 400);
            accountBalance.setFont(new Font("TimesRoman", Font.BOLD, 140));
            panel.add(accountBalance);

            f.setVisible(true);
        }
        rs.close();
    }

    public void window() {
        f = new JFrame("MonIng");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(width, height);
        panel.setLayout(null);
        f.setLocationRelativeTo(null);
        f.add(panel);
    }

    public void labels() {
        accountBalanceText = new JLabel("Masz na koncie:");
        accountBalanceText.setBounds(100, 400, 310, 40);
        accountBalanceText.setFont(new Font("TimesRoman", Font.BOLD, 30));
        panel.add(accountBalanceText);
    }

}
public class Window extends JFrame {

    private static final int width = 1280;
    private static final int height = 720;

    JFrame f;
    JFrame f2;
    JPanel panel = new JPanel();
    JPanel panel2 = new JPanel();

    JButton test;
    JButton test2;
    JButton test3;
    JButton test4;

    JLabel accountBalanceText;
    JLabel accountBalance;
    JLabel possibleRelease;
    JLabel spendMoney;

    JTextArea spendMoneyText;
    JTextArea testText;

    public Window() {
    }

    public void start() {
        Frame();
        textFields();
        buttons();
        actions();
        labels();
        setVisible(true);
    }

    public void Frame() {
        f = new JFrame("MonIng");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(width, height);
        panel.setLayout(null);
        setLocationRelativeTo(null);
        add(panel);

    }

    public void labels() {

        accountBalanceText = new JLabel("Masz na koncie:");
        accountBalanceText.setBounds(100, 400, 310, 40);
        accountBalanceText.setFont(new Font("TimesRoman", Font.BOLD, 30));
        panel.add(accountBalanceText);

        accountBalance = new JLabel("asd" + "zł");
        accountBalance.setBounds(100, 300, 800, 400);
        accountBalance.setFont(new Font("TimesRoman", Font.BOLD, 140));
        panel.add(accountBalance);

        possibleRelease = new JLabel("Dziś możesz wydać: " + "asd" + " zł");
        possibleRelease.setBounds(700, 50, 400, 30);
        possibleRelease.setFont(new Font("TimesRoman", Font.BOLD, 23));
        panel.add(possibleRelease);

        spendMoney = new JLabel("Dziś wydane: ");
        spendMoney.setBounds(700, 125, 400, 30);
        spendMoney.setFont(new Font("TimesRoman", Font.BOLD, 23));
        panel.add(spendMoney);

    }

    public void buttons() {
        test = new JButton("test");
        test.setBounds(10, 10, 200, 50);
        panel.add(test);

        test2 = new JButton("dodaj");
        test2.setBounds(200, 10, 200, 50);
        panel.add(test2);

        test3 = new JButton("balance");
        test3.setBounds(400, 10, 200, 50);
        panel.add(test3);

        test4 = new JButton("konto bankowe");
        test4.setBounds(700, 10, 200, 50);
        panel.add(test4);

    }

    public void textFields() {
        spendMoneyText = new JTextArea();
        spendMoneyText.setBounds(500, 500, 200, 30);
        spendMoneyText.setFont(new Font("TimesRoman", Font.BOLD, 23));
        panel.add(spendMoneyText);

        testText = new JTextArea();
        testText.setBounds(500, 400, 200, 30);
        testText.setFont(new Font("TimesRoman", Font.BOLD, 23));
        testText.setText("asdg");
        panel.add(testText);

    }

    public void actions() {
        test.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                displayData();
            }
        });

        test2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!spendMoneyText.getText().isEmpty()) {
                    insertInto();
                    System.out.println("as");
                } else {
                    JOptionPane.showMessageDialog(null, "Pole nie może byc puste!");
                }
            }
        });
        test3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if (!testText.getText().isEmpty()) {
                        getBalance(testText.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "Pole nie może byc puste!");
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });

        test4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Bank b = new Bank();

                try {
                    b.start();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });

    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0