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