Getting Null Pointer Exception in Java Swing - java

Above is my code with I made to database and swing. After running it, when I click on the remove button after entering the id, I am getting some null pointer exception. I have created a table with id, name and price. The part where the book add feature is present in working perfectly fine. Only the part where I can remove the book using prepared statement is not working properly. Below is the code and the exception.
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
class showbook extends JPanel
{
JButton b1;
showbook()
{
setLayout(null);
setBackground(Color.cyan);
JLabel l1 = new JLabel("Book List");
b1 = new JButton("Display Book");
l1.setBounds(250, 20, 400, 20);
b1.setBounds(207 ,70, 150 ,20);
add(l1);
add(b1);
}
}
class addbook extends JPanel implements ActionListener
{
JTextField t1;
JTextField t2;
JTextField t3;
addbook()
{
setLayout(null);
setBackground(Color.cyan);
JLabel l1 = new JLabel("Add books from here!!!");
JLabel l2 = new JLabel("Book ID:");
t1 = new JTextField();
JLabel l3 = new JLabel("Book Name:");
t2 = new JTextField();
JLabel l4 = new JLabel("Book Price:");
t3 = new JTextField();
JButton b1 = new JButton("Add");
l1.setBounds(250, 20, 400, 20);
l2.setBounds(200, 100, 100, 20);
t1.setBounds(280, 100, 150, 20);
l3.setBounds(200, 150, 100, 20);
t2.setBounds(280, 150, 150, 20);
l4.setBounds(200, 200, 100, 20);
t3.setBounds(280, 200, 150, 20);
b1.setBounds(250 ,250, 150 ,20);
add(l1);
add(l2);
add(t1);
add(l3);
add(t2);
add(l4);
add(t3);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s1= t1.getText();
String s2= t2.getText();
String s3= t3.getText();
String q ="insert into book values(?,?,?)";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","system","system");
PreparedStatement stmt=con.prepareStatement(q);
stmt.setString(1,s1);
stmt.setString(2,s2);
stmt.setString(3,s3);
stmt.executeUpdate();
con.close();
}
catch(Exception ae)
{
System.out.println(ae);
}
}
}
class removebook extends JPanel implements ActionListener
{
JTextField t1;
removebook()
{
setLayout(null);
setBackground(Color.cyan);
JLabel l1 = new JLabel("Add books from here!!!");
JLabel l2 = new JLabel("Book ID:");
JTextField t1 = new JTextField();
JButton b1 = new JButton("Remove");
l1.setBounds(250, 20, 400, 20);
l2.setBounds(200, 100, 100, 20);
t1.setBounds(280, 100, 150, 20);
b1.setBounds(250 ,150, 150 ,20);
add(l1);
add(l2);
add(t1);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String s1= t1.getText();
String q ="delete from book where id = ?";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","system","system");
PreparedStatement stmt=con.prepareStatement(q);
stmt.setString(1,s1);
stmt.executeUpdate();
con.close();
}
catch(Exception ae)
{
System.out.println(ae);
}
}
}
class dbproject extends JFrame
{
dbproject()
{
Container c = getContentPane();
JTabbedPane jtp=new JTabbedPane();
showbook p1 = new showbook();
addbook p2 = new addbook();
removebook p3 = new removebook();
jtp.addTab("Show Books", p1);
jtp.addTab("Add Books", p2);
jtp.addTab("Remove Books", p3);
c.add(jtp);
}
public static void main(String args[])
{
dbproject f1 = new dbproject();
f1.setTitle("Too much Kashta");
f1.setVisible(true);
f1.setSize(600,600);
}
}
Bellow is the exception I am getting in the console
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at removebook.actionPerformed(dbproject.java:108)
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:262)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3318)
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)

Related

Having a Null pointer exception while coding my first GUI and I can't figure out where it's going wrong [duplicate]

This question already has answers here:
How to best position Swing GUIs?
(2 answers)
Closed 3 years ago.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class JFrameTest {
private MyListener listener = new MyListener();
private JButton clear = null;
private JButton addOne = null;
private static JFrame frame = null;
private JTextField text1 = null;
private JTextField text2 = null;
private JTextField text3 = null;
private JTextField text4 = null;
private JTextField text5 = null;
private JTextField text6 = null;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("Button & Listener Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JFrameTest().createGUI());
//Display the window.
frame.setSize(800, 500);
frame.setVisible(true);
}
public JTabbedPane createGUI(){
JTabbedPane tabbedPane = new JTabbedPane();
JPanel one = new JPanel();
one.setLayout(new BorderLayout());
JPanel test = new JPanel();
JPanel test2 = new JPanel();
JLabel label1 = new JLabel ("Pound");
JLabel label2 = new JLabel("Ounce :" );
JLabel label3 = new JLabel("Ton :");
JLabel label4 = new JLabel("Tonne :");
JLabel label5 = new JLabel("Kilogram :");
JLabel label6 = new JLabel("Gram :");
text1 = new JTextField(5);
text2 = new JTextField(5);
text3 = new JTextField(5);
text4 = new JTextField(5);
text5 = new JTextField(5);
text6 = new JTextField(5);
JButton button = new JButton();
clear = new JButton("Clear");
clear.addActionListener(listener);
text1.setText("0");
text2.setText("0");
text3.setText("0");
text4.setText("0");
text5.setText("0");
text6.setText("0");
test.add(label1);
test.add(text1);
test.add(label2);
test.add(text2);
test.add(label3);
test.add(text3);
test2.add(label4);
test2.add(text4);
test2.add(label5);
test2.add(text5);
test2.add(label6);
test2.add(text6);
one.add(test, BorderLayout.NORTH);
one.add(test2, BorderLayout.CENTER);
one.add(clear, BorderLayout.SOUTH);
tabbedPane.addTab("Tab 1", one);
JPanel two = new JPanel();
tabbedPane.addTab("Tab 2", two);
return tabbedPane;
}
public class MyListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton)e.getSource();
String sourceName = source.getName();
if(sourceName.equals("Clear"))
{
System.out.println("yeet");
text1.setText("00");
text2.setText("00");
text3.setText("00");
text4.setText("00");
text5.setText("00");
text6.setText("00");
}
else
{
System.out.println("nah");
}
}
}
}
And my error code is:Exception in thread "AWT-EventQueue-0"
java.lang.NullPointerException
at jframetest.JFrameTest$MyListener.actionPerformed(JFrameTest.java:123)
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:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
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:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
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)
The only two usages of the JButton are basically
clear = new JButton("Clear");
clear.addActionListener(listener);
So, you aren't setting a name for the Component.
Here sourceName is null.
String sourceName = source.getName();
And here is your NullPointerException.
sourceName.equals("Clear")
A general tip, when comparing Strings, use always the one which is never null to perform the operation, in this case
if ("Clear".equals(sourceName)) { ... }
The Component#getName method (inherited) may seem to construct a name for you, if it is null
if (name == null && !nameExplicitlySet) {
synchronized(getObjectLock()) {
if (name == null && !nameExplicitlySet)
name = constructComponentName();
}
}
return name;
But, constructComponentName returns null
String constructComponentName() {
return null; // For strict compliance with prior platform versions, a Component
// that doesn't set its name should return null from
// getName()
}
It is because you are not setting the .name property of the button, just the text property. The default constructor set the text property not the name.
You can set the name property of the button.
clear = new JButton("Clear");//this just set the text property
clear.setName("Clear"); //here
Or just change your if condition like
String sourceName = source.getText(); //chage .getName for .getText
if(sourceName.equals("Clear")) //now will work

what does java.awt.EventDispatchThread.pumpEvents mean?

we have a problem with our GUI code. When we run it several errors occurs and we have no idea what they mean. We are creating an ordersystem from a model and then we have to include an interface that we build in Window builder. Very new to java programming so we need all help we can get:D thanks.
GUI code:
package ISprojekt3;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class OrderApplication {
Controller1 controller = new Controller1();
private JFrame frame;
private JTextField name;
private JTextField customerNumber;
private JTextField address;
private JTextField deliveryDate;
private JTextField orderID;
private JTextField number;
private JTextField quantity;
private JTextField nameProduct;
private JTextField category;
private JTextField price;
private JTextField serialNumber;
private JTextArea message;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OrderApplication window = new OrderApplication();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public OrderApplication() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 672, 482);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblName = new JLabel("Namn:");
lblName.setBounds(23, 45, 61, 16);
frame.getContentPane().add(lblName);
JLabel lblCustomerNumber = new JLabel("Kundnummer:");
lblCustomerNumber.setBounds(23, 73, 106, 16);
frame.getContentPane().add(lblCustomerNumber);
JLabel lblAddress = new JLabel("Adress:");
lblAddress.setBounds(23, 103, 61, 16);
frame.getContentPane().add(lblAddress);
JLabel lblCustomer = new JLabel("Kund");
lblCustomer.setBounds(23, 17, 61, 16);
frame.getContentPane().add(lblCustomer);
name = new JTextField();
name.setBounds(125, 40, 130, 26);
frame.getContentPane().add(name);
name.setColumns(10);
customerNumber = new JTextField();
customerNumber.setBounds(125, 68, 130, 26);
frame.getContentPane().add(customerNumber);
customerNumber.setColumns(10);
address = new JTextField();
address.setBounds(125, 98, 130, 26);
frame.getContentPane().add(address);
address.setColumns(10);
JLabel lblOrder = new JLabel("Order");
lblOrder.setBounds(313, 17, 61, 16);
frame.getContentPane().add(lblOrder);
JLabel lblOrderID = new JLabel("OrderID:");
lblOrderID.setBounds(313, 45, 61, 16);
frame.getContentPane().add(lblOrderID);
JLabel lblDeliveryDate = new JLabel("Leveransdatum:");
lblDeliveryDate.setBounds(313, 73, 106, 16);
frame.getContentPane().add(lblDeliveryDate);
deliveryDate = new JTextField();
deliveryDate.setBounds(412, 68, 130, 26);
frame.getContentPane().add(deliveryDate);
deliveryDate.setColumns(10);
orderID = new JTextField();
orderID.setBounds(412, 40, 130, 26);
frame.getContentPane().add(orderID);
orderID.setColumns(10);
JLabel lblOrderLine = new JLabel("Orderrad");
lblOrderLine.setBounds(313, 159, 61, 16);
frame.getContentPane().add(lblOrderLine);
JLabel lblNumber = new JLabel("Nummer:");
lblNumber.setBounds(313, 187, 61, 16);
frame.getContentPane().add(lblNumber);
JLabel lblQuantity = new JLabel("Antal produkter:");
lblQuantity.setBounds(313, 215, 106, 16);
frame.getContentPane().add(lblQuantity);
number = new JTextField();
number.setBounds(412, 182, 130, 26);
frame.getContentPane().add(number);
number.setColumns(10);
quantity = new JTextField();
quantity.setBounds(412, 210, 130, 26);
frame.getContentPane().add(quantity);
quantity.setColumns(10);
JLabel lblProduct = new JLabel("Produkt");
lblProduct.setBounds(23, 159, 61, 16);
frame.getContentPane().add(lblProduct);
JLabel lblName_2 = new JLabel("Namn:");
lblName_2.setBounds(23, 187, 61, 16);
frame.getContentPane().add(lblName_2);
JLabel lblCategory = new JLabel("Kategori:");
lblCategory.setBounds(23, 215, 61, 16);
frame.getContentPane().add(lblCategory);
nameProduct = new JTextField();
nameProduct.setBounds(125, 187, 130, 26);
frame.getContentPane().add(nameProduct);
nameProduct.setColumns(10);
category = new JTextField();
category.setBounds(125, 215, 130, 26);
frame.getContentPane().add(category);
category.setColumns(10);
price = new JTextField();
price.setBounds(125, 238, 130, 26);
frame.getContentPane().add(price);
price.setColumns(10);
JLabel lblPrice = new JLabel("Pris:");
lblPrice.setBounds(23, 243, 61, 16);
frame.getContentPane().add(lblPrice);
JLabel lblCopy = new JLabel("Exemplar");
lblCopy.setBounds(23, 297, 69, 16);
frame.getContentPane().add(lblCopy);
JLabel lblSerialNumber = new JLabel("Serienummer:");
lblSerialNumber.setBounds(23, 325, 106, 16);
frame.getContentPane().add(lblSerialNumber);
serialNumber = new JTextField();
serialNumber.setBounds(125, 320, 130, 26);
frame.getContentPane().add(serialNumber);
serialNumber.setColumns(10);
JButton btnSearch = new JButton("Sök");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnSearch.setBounds(409, 263, 117, 29);
frame.getContentPane().add(btnSearch);
JButton btnAdd = new JButton("Lägg till");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String customerName = name.getText();
String customerNumber = number.getText();
String customerAddress = address.getText();
if((customerName.length() < 1) || (customerNumber.length() < 1) || (customerAddress.length() < 1)) {
message.setText("Tilläggning misslyckades");
}
else {
controller.addCustomer(customerNumber, customerName, customerAddress);
message.setText("Kunden är tillagd");
}
String order = orderID.getText();
String dDate = deliveryDate.getText();
if((order.length() < 1 ) || (dDate.length() < 1)) {
message.setText("Tilläggning misslyckades");
}
else {
controller.addOrder(order, dDate, customerNumber);
message.setText("Ordern är tillagd till kund " + controller.findCustomer(customerNumber).getCustomerNumber());
}
String productName = nameProduct.getText();
String productCategory = category.getText();
String productPrice = price.getText();
if((productName.length() < 1 ) || (productCategory.length() < 1 ) || (productPrice.length() < 1 )) {
message.setText("Tilläggning misslyckades");
}
else {
controller.addProduct(productName, productCategory, productPrice);
message.setText("Produkten är tillagd");
}
String orderLineNumber = number.getText();
String orderLineQuantity = quantity.getText();
if((orderLineNumber.length() < 1 ) || (orderLineQuantity.length() < 1)) {
message.setText("Tilläggning misslyckades");
}
else {
controller.addOrderLine(orderLineNumber, orderLineQuantity, order);
message.setText("Orderraden är tillagd i order" + controller.findOrder(order).getOrderID());
}
String serialNumberCopy = serialNumber.getText();
if((serialNumberCopy.length() < 1)) {
message.setText("Tilläggningen misslyckades");
}
else {
controller.addCopy(serialNumberCopy, productName);
message.setText("Exemplaret är tillagd i produkt " + controller.findProduct(productName).getName());
}
}
});
btnAdd.setBounds(409, 292, 117, 29);
frame.getContentPane().add(btnAdd);
JButton btnRemove = new JButton("Ta bort");
btnRemove.setBounds(409, 370, 117, 29);
frame.getContentPane().add(btnRemove);
JButton btnChange = new JButton("Ändra");
btnChange.setBounds(409, 345, 117, 29);
frame.getContentPane().add(btnChange);
JTextArea message = new JTextArea();
message.setBounds(23, 375, 231, 50);
frame.getContentPane().add(message);
}
}
And here are the errors:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ISprojekt3.OrderApplication$3.actionPerformed(OrderApplication.java:198)
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:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
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:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
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)
Use messed up with variable scopes and the used field private JTextArea message was not initialized.
The created local scoped one JTextArea message = new JTextArea() was never assigned to the field message.

button not showing in Jframe

btn3 is not showing when I run it in Netbeans.
when i ran it in NotePad it shows third button.
i cant really figure what the problem is.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Regform2 extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField tf1, tf2, tf5, tf6, tf7;
JButton btn1, btn2, btn3;
JPasswordField p1, p2;
Regform2() {
setTitle("Registration Form in Java");
l1 = new JLabel("Registration Form in Windows Form:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Create Passowrd:");
l5 = new JLabel("Confirm Password:");
l6 = new JLabel("Country:");
l7 = new JLabel("State:");
l8 = new JLabel("Phone No:");
tf1 = new JTextField();
tf2 = new JTextField();
p1 = new JPasswordField();
p2 = new JPasswordField();
tf5 = new JTextField();
tf6 = new JTextField();
tf7 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn3 = new JButton("nxt");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
l4.setBounds(80, 150, 200, 30);
l5.setBounds(80, 190, 200, 30);
l6.setBounds(80, 230, 200, 30);
l7.setBounds(80, 270, 200, 30);
l8.setBounds(80, 310, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
p1.setBounds(300, 150, 200, 30);
p2.setBounds(300, 190, 200, 30);
tf5.setBounds(300, 230, 200, 30);
tf6.setBounds(300, 270, 200, 30);
tf7.setBounds(300, 310, 200, 30);
btn1.setBounds(50, 350, 100, 30);
btn2.setBounds(170, 350, 100, 30);
btn3.setBounds(300, 350, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(l4);
add(p1);
add(l5);
add(p2);
add(l6);
add(tf5);
add(l7);
add(tf6);
add(l8);
add(tf7);
add(btn1);
add(btn2);
add(btn3);
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
int x = 0;
String s1 = tf1.getText();
String s2 = tf2.getText();
char[] s3 = p1.getPassword();
char[] s4 = p2.getPassword();
String s8 = new String(s3);
String s9 = new String(s4);
String s5 = tf5.getText();
String s6 = tf6.getText();
String s7 = tf7.getText();
if (s8.equals(s9)) {
JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");
} else {
JOptionPane.showMessageDialog(btn1, "Password Does Not Match");
}
} else if (e.getSource() == btn2) {
tf1.setText("");
tf2.setText("");
p1.setText("");
p2.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
} else {
this.dispose();
NewJFrame nw = new NewJFrame();
nw.setVisible(true);
}
}
public static void main(String args[]) {
new Regform2();
}}
if you put setlayout(null) at first then setVisible(true),the btn3 will show in jframe. like this:
setLayout(null);
setVisible(true);
setSize(700, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
You need to add a JPanel. To this panel you add your components.
And don't forget to add your panel to your frame like this:
JPanel contentPane = new JPanel();
content.add(new JButton("Test"));
this.setDefaultCloseOperation(3);
this.setContentPane(contentPanel);
this.setSize(Width, Height);
this.setResizable(false);
this.setVisible(true);
this code will work... please define the codes of main frame at beginning of the
constructor, such as
setTitle();
setSize();
setLayout();
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Regform2 extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField tf1, tf2, tf5, tf6, tf7;
JButton btn1, btn2, btn3;
JPasswordField p1, p2;
Regform2() {
setTitle("Registration Form in Java");
setSize(700, 700);
setLayout(null);
l1 = new JLabel("Registration Form in Windows Form:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Create Passowrd:");
l5 = new JLabel("Confirm Password:");
l6 = new JLabel("Country:");
l7 = new JLabel("State:");
l8 = new JLabel("Phone No:");
tf1 = new JTextField();
tf2 = new JTextField();
p1 = new JPasswordField();
p2 = new JPasswordField();
tf5 = new JTextField();
tf6 = new JTextField();
tf7 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn3 = new JButton("Next");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
l4.setBounds(80, 150, 200, 30);
l5.setBounds(80, 190, 200, 30);
l6.setBounds(80, 230, 200, 30);
l7.setBounds(80, 270, 200, 30);
l8.setBounds(80, 310, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
p1.setBounds(300, 150, 200, 30);
p2.setBounds(300, 190, 200, 30);
tf5.setBounds(300, 230, 200, 30);
tf6.setBounds(300, 270, 200, 30);
tf7.setBounds(300, 310, 200, 30);
btn1.setBounds(50, 350, 100, 30);
btn2.setBounds(170, 350, 100, 30);
btn3.setBounds(290, 350, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(l4);
add(p1);
add(l5);
add(p2);
add(l6);
add(tf5);
add(l7);
add(tf6);
add(l8);
add(tf7);
add(btn1);
add(btn2);
add(btn3);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
int x = 0;
String s1 = tf1.getText();
String s2 = tf2.getText();
char[] s3 = p1.getPassword();
char[] s4 = p2.getPassword();
String s8 = new String(s3);
String s9 = new String(s4);
String s5 = tf5.getText();
String s6 = tf6.getText();
String s7 = tf7.getText();
if (s8.equals(s9)) {
JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");
} else {
JOptionPane.showMessageDialog(btn1, "Password Does Not Match");
}
} else if (e.getSource() == btn2) {
tf1.setText("");
tf2.setText("");
p1.setText("");
p2.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
} else {
this.dispose();
//NewJFrame nw = new NewJFrame();
//nw.setVisible(true);
}
}
public static void main(String args[]) {
new Regform2();
}
}

retrieve mysql data when i change combobox item in java

I use the following code to retrieve data from msysql database to a JComboBox. It was working properly. My problem is when I execute this code, if I click jCombobox, only one item is shown and it displays the related data from database.. it is not getting all the ids from database to Combobox, so that i can select a particular id and display related data.
package Swings;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.*;
import javax.swing.*;
class Frame3 extends JFrame implements ActionListener,ItemListener
{
private JFrame frame;
JPanel panel=new JPanel();
JButton btn1,btn2,btn3;
JLabel l1, l2, l3, l4, l5, l6, l7;
private JComboBox SummonIdbox;
private JTextField txtcarLicenceNo;
private JTextField txtamount;
private JTextField txtdateFined;
private JTextField txtlocation;
private JTextField txtofficerInCharge;
String dbURL = "jdbc:mysql://localhost:3306/cpss";
String username ="root";
String password = "root";
Connection c = null;
Statement st = null;
ResultSet rs = null;
Frame3()
{
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("CPSS Application");
l1 = new JLabel("Update Summon Details");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("SummonId");
l3 = new JLabel("Car LicenceNo:");
l4 = new JLabel("Amount");
l5 = new JLabel("DateFined");
l6 = new JLabel("Location");
l7 = new JLabel("Officer Incharge");
SummonIdbox = new JComboBox();
txtcarLicenceNo = new JTextField();
txtamount = new JTextField();
txtdateFined = new JTextField();
txtlocation = new JTextField();
txtofficerInCharge=new JTextField();
btn1 = new JButton("Update");
btn2 = new JButton("Clear");
btn3= new JButton("Home");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
SummonIdbox.addItemListener(this);
l1.setBounds(500, 30, 400, 30);
l2.setBounds(400, 70, 200, 30);
l3.setBounds(400, 110, 200, 30);
l4.setBounds(400, 150, 200, 30);
l5.setBounds(400, 190, 200, 30);
l6.setBounds(400, 230, 200, 30);
l7.setBounds(400, 270, 200, 30);
SummonIdbox.setBounds(500, 70, 200, 30);
txtcarLicenceNo.setBounds(500, 110, 200, 30);
txtamount.setBounds(500, 150, 200, 30);
txtdateFined.setBounds(500, 190, 200, 30);
txtlocation.setBounds(500, 230, 200, 30);
txtofficerInCharge.setBounds(500, 270, 200, 30);
btn1.setBounds(450, 350, 100, 30);
btn2.setBounds(600, 350, 100, 30);
btn3.setBounds(750, 350, 100, 30);
add(l1);
add(l2);
add(SummonIdbox);
add(l3);
add(txtcarLicenceNo);
add(l4);
add(txtamount);
add(l5);
add(txtdateFined);
add(l6);
add(txtlocation);
add(l7);
add(txtofficerInCharge);
add(btn1);
add(btn2);
add(btn3);
add_summonid(SummonIdbox);
}
#Override
public void actionPerformed(ActionEvent ae ) {
if(ae.getSource() == btn2)
{
txtcarLicenceNo.setText("");
txtamount.setText("");
txtdateFined.setText("");
txtlocation.setText("");
txtofficerInCharge.setText("");
}
else if (ae.getSource() == btn3)
{
SwingMenu s=new SwingMenu();
s.setSize(800,800);
s.setVisible(true);
}
}
void add_summonid(JComboBox SummonIdbox)
{
try
{
//Class.forName("com.mysql.jdbc.Driver");
//System.out.println("Driver is loaded");
c = DriverManager.getConnection(dbURL, username, password);
System.out.println("Connection created");
st=c.createStatement();
rs=st.executeQuery("select Summonid from summon");
while(rs.next())
SummonIdbox.addItem(rs.getInt(1));
// c.close();
}
catch (Exception ex)
{
System.out.println(ex);
}
}
#Override
public void itemStateChanged(ItemEvent ie) {
int item= (Integer) SummonIdbox.getSelectedItem();
System.out.println(item);
String sql="select carLicenceNo,amount,dateFined,location,officerIncharge from summon where summonId='"+item+"'";
try {
PreparedStatement pst = c.prepareStatement(sql);
//pst.setInt(1,item);
rs=pst.executeQuery();
while(rs.next())
{
txtcarLicenceNo.setText(rs.getString(1));
txtamount.setText(String.valueOf((rs.getDouble(2))));
txtdateFined.setText(rs.getString(3));
txtlocation.setText(rs.getString(4));
txtofficerInCharge.setText(rs.getString(5));
/*System.out.println(rs.getString(1));
System.out.println(rs.getDouble(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));*/
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Java frame trouble

I need help with my program because I'll want to put a password and username to my program, so if the username is test && password is 12345 my program will appear a new frame but unfortunately my second didn't work for my label, button and etc here is my code so far.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwapFields extends JFrame{
public static void main(String[] args) {
SwapFields frameTabel = new SwapFields();
}
JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);
JLabel lab = new JLabel("Username :");
JLabel pas = new JLabel("Password :");
JLabel cos;
// JPanel panel = new JPanel();
JButton y1;
JButton y2;
SwapFields() {
super("Enter Your Account !");
setSize(300, 200);
setLocation(500, 280);
panel.setLayout(null);
txuser.setBounds(90, 30, 150, 20);
pass.setBounds(90, 65, 150, 20);
blogin.setBounds(110, 100, 80, 20);
lab.setBounds(15, 28, 150, 20);
pas.setBounds(15, 63, 150, 20);
panel.add(lab);
panel.add(pas);
panel.add(blogin);
panel.add(txuser);
panel.add(pass);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
}
public void actionlogin() {
blogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String puname = txuser.getText();
String ppaswd = pass.getText();
if (puname.equals("test") && ppaswd.equals("12345")) {
JFrame frame = new JFrame("Customer");
JPanel panel1 = new JPanel();
frame.setVisible(true);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cos = new JLabel("Do you have a Customer ?");
y1 = new JButton("Yes");
y2 = new JButton("No");
panel1.setLayout(null);
cos.setBounds(70, 30, 150, 20);
y1.setBounds(80, 65, 150, 20);
y2.setBounds(140, 65, 150, 20);
y1.setSize(55, 30);
y2.setSize(55, 30);
panel1.add(y1);
panel1.add(y2);
panel1.add(cos);
frame.getContentPane().add(panel1);
// this is the code ill change this is the second frame .
dispose();
} else {
JOptionPane.showMessageDialog(null, "Wrong Password / Username");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}
}
});
}
}
change
panel.setLayout(null);
to
panel.setLayout(new Flowlayout());

Categories