Exception in thread "AWT-EventQueue-0" - Java [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
when I try to run this program the following error message appears:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Quiz1.<init>(Quiz1.java:21)
at Opening_Screen.jButton1ActionPerformed(Opening_Screen.java:138)
at Opening_Screen.access$000(Opening_Screen.java:16)
at Opening_Screen$1.actionPerformed(Opening_Screen.java:57)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
As I'm extremely new to Java I have absolutely no idea what any of this means, if someone could show me where in my code this is happening I'd be extremely appreciative!
Here is the code.
public class Quiz1 extends javax.swing.JFrame {
ArrayList <String> quiz1Answers = new ArrayList();
ArrayList <String> quiz1UserAnswers = new ArrayList();
String q1 = this.txtInputQ1.getText();
String q2 = this.txtInputQ2.getText();
String q3 = this.txtInputQ3.getText();
String q4 = this.txtInputQ4.getText();
String q5 = this.txtInputQ5.getText();
String q6 = this.txtInputQ6.getText();
String q7 = this.txtInputQ7.getText();
String q8 = this.txtInputQ8.getText();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null, "This is a practice quize to test what you have learned.", "Instructions", + JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "<html>To check your answers press the <b><i>'Check'</b></i> button and enter which question to check", "Instructions", + JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "<html>When completed click the <b><i>'Start Quiz'</b></i> button", "Instructions", + JOptionPane.INFORMATION_MESSAGE);
}
private void txtInputQ6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (q1.equals("") || q2.equals("") || q3.equals("") || q4.equals("") || q5.equals("") || q6.equals("") || q7.equals("") || q8.equals(""))
{
JOptionPane.showMessageDialog(null, "Make sure all fields are filled in!", "Error!", + JOptionPane.ERROR_MESSAGE);
}
else
{
quiz1UserAnswers.add(q1);
quiz1UserAnswers.add(q2);
quiz1UserAnswers.add(q3);
quiz1UserAnswers.add(q4);
quiz1UserAnswers.add(q5);
quiz1UserAnswers.add(q6);
quiz1UserAnswers.add(q7);
quiz1UserAnswers.add(q8);
JOptionPane.showMessageDialog(null, "<html>Press the <b><i>'Check Answer'</b></i> button now!", "", + JOptionPane.INFORMATION_MESSAGE);
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
Collections.addAll(quiz1Answers, "ottawa", "toronto", "tokyo", "northamerica", "seoul", "Asia", "unitedstates", "50");
}
Thank you in advance!

I'm probably going to assume that you're trying to get the values before even initializing the object. Make sure that the object txtInputQ1 till txtInputQ2 is first created before you call the .getText(); method.

Related

I got an exception in evt Java netbeans [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I think, I got an error in this part, but I don't know to repair it. The result of error is
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at bismillahpata.View.hitungHPP1.selanjutnyaActionPerformed(hitungHPP1.java:381)
at bismillahpata.View.hitungHPP1.access$800(hitungHPP1.java:28)
at bismillahpata.View.hitungHPP1$9.actionPerformed(hitungHPP1.java:330)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
and this my code
private void selanjutnyaActionPerformed(java.awt.event.ActionEvent evt) {
String namaProduk = this.fieldNamaProduk.getText();
double totalProduk = Double.parseDouble(fieldTotalProduk.getText());
produk p = new produk();
p.setNamaProduk(namaProduk);
p.setJumlah(totalProduk);
produkController pc = new produkController();
int res = pc.insertProduct(p);
if (res > 0){
new hitungHPP(namaProduk, totalProduk).setVisible(true);
this.dispose();
}else{
JOptionPane.showMessageDialog(null, "Perintah Gagal, silahkan isi ulang!");
}
totalHargaBahan = totalHargaBahan + total;
System.out.println(totalHargaBahan);
}
You have to use try catch with code,
private void selanjutnyaActionPerformed(java.awt.event.ActionEvent evt) {
String namaProduk = this.fieldNamaProduk.getText();
double totalProduk = Double.parseDouble(fieldTotalProduk.getText());
produk p = new produk();
p.setNamaProduk(namaProduk);
p.setJumlah(totalProduk);
produkController pc = new produkController();
int res = pc.insertProduct(p);
if (res > 0){
try{
new hitungHPP(namaProduk, totalProduk).setVisible(true);
this.dispose();
}
catch(Exception e)
{
System.out.println(e);
}
}else{
JOptionPane.showMessageDialog(null, "Perintah Gagal, silahkan isi ulang!");
}
totalHargaBahan = totalHargaBahan + total;
System.out.println(totalHargaBahan);
}

Simple Delete Query throwing Null Pointer Exception in Java Derby [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am using NetBeans IDE 8.1 and trying to delete a product using the product id in the database. But the I am getting a runtime error
java.lang.NullPointerException
at ambeysofas.Function.deleteProduct(Function.java:220)
at ambeysofas.AmbeySofas.btnDeleteProductActionPerformed(AmbeySofas.java:866)
at ambeysofas.AmbeySofas.access$500(AmbeySofas.java:19)
at ambeysofas.AmbeySofas$7.actionPerformed(AmbeySofas.java:550)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
The Line Number 220 is the Delete Query itself. Following is the code of the function deleteProduct
public boolean deleteProduct(int id){
try {
conn.createStatement();
try{
stmt.execute("DELETE FORM tbl_products WHERE product_id="+id);
b=true;
}catch(Exception e){
System.out.print(e);
e.printStackTrace();
b=false;
}
} catch (SQLException ex) {
System.out.println(ex);
Logger.getLogger(Function.class.getName()).log(Level.SEVERE, null, ex);
b=false;
}
return b;
}
The function is called from another file. the code of the part is given below:
private void btnDeleteProductActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String str = JOptionPane.showInputDialog(this, "Enter Product ID", "Prompt", JOptionPane.OK_CANCEL_OPTION);
try {
ResultSet rs1 = new Function().fetchProductByID(Integer.parseInt(str));
while (rs1.next()) {
int r = JOptionPane.showConfirmDialog(this, rs1.getString("product_name") + " | " + rs1.getString("product_type"), "Confirm Deletion of Product:" + str, JOptionPane.OK_CANCEL_OPTION);
if (r == 0) {
if (new Function().deleteProduct(Integer.parseInt(str))) {
JOptionPane.showMessageDialog(this, "Product Deleted Successfully!", "Success!", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Something Went Wrong!", "Oops!", JOptionPane.ERROR_MESSAGE);
}
}
}
} catch (Exception e) {
}
}
The code generates the exception when the delete query is executed. I'll be thankful if anyone can suggest some solution.
maybe just a miss in you query
FORM despite of FROM
Enjoy

How to change icon of label with mouse entered event?

Here is the code of mouse entered event of label 1 . And the exception is occuring .I have already worked on previous version but I don't why this happening now.
private void jLabel1MouseEntered(java.awt.event.MouseEvent evt) {
ImageIcon n_cig;
n_cig = new ImageIcon(getClass().getResource("masterproject/Cfull.png"));
jLabel1.setIcon(n_cig);
}
private void jLabel1MouseExited(java.awt.event.MouseEvent evt) {
ImageIcon n_cig;
n_cig = new ImageIcon(getClass().getResource("masterproject/No Cigarretes.png"));
jLabel1.setIcon(n_cig);
}
And the exceptions are here
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at masterproject.design.jButton1ActionPerformed(design.java:384)
at masterproject.design.access$300(design.java:15)
at masterproject.design$3.actionPerformed(design.java:148)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
The reason that you get a NullPointerException is because for some reason the image file that you're trying to specify cannot be located. So the getResource() method returns a null.
Create a utility function to handle these kind of exception in future
public ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
Check using this function that wheather or not you are able to access the image or not

How can I show a JTable in a dialog box in NetBeans?

I have had a few problems trying to get this table to show in NetBeans. Here is the code that I am trying to use. I am aware that the loop is wrong and will fix that at a later date.
I get a runtime error with the following code and I can't work it out.
Object[][] data = null;
String[] columnNames = {"Type", "Grade", "Height", "Width", "Length", "Reinforced Bottom", "Reinforced Corners", "Sealable", "Quantity"};
JTable table = new JTable();
public void showTotalOrder(){
for(int i=0; i<boxes.size(); i++){
for(int j=0; j<1; j++){
data[i][j] = boxes.get(i).getName();
data[i][j+1] = boxes.get(i).getGrade();
data[i][j+2] = boxes.get(i).getHeight();
data[i][j+3] = boxes.get(i).getWidth();
data[i][j+4] = boxes.get(i).getLength();
data[i][j+5] = boxes.get(i).getReinforcedBottom();
data[i][j+6] = boxes.get(i).getReinforcedCorners();
data[i][j+7] = boxes.get(i).getSealable();
data[i][j+8] = boxes.get(i).getQuantity();
}
}
JOptionPane.showMessageDialog(this, table);
}
I believe the problem is with the last line of this method.
The error i receive is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at coursework.FlexBox.showTotalOrder(FlexBox.java:116)
at coursework.FlexBox.currentOrderButtonActionPerformed(FlexBox.java:530)
at coursework.FlexBox.access$300(FlexBox.java:18)
at coursework.FlexBox$4.actionPerformed(FlexBox.java:435)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
I get a runtime error with the following code and I can't work it out.
Given this stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at coursework.FlexBox.showTotalOrder(FlexBox.java:116)
...
You get a NullPointerException because of data array is initialized to null:
Object[][] data = null;
Please have a read to:
What is a Null Pointer Exception, and how do I fix it?
Creating, Initializing, and Accessing an Array

How to only select one item from a number of JLists in Swing

So i have a GUI which creates Jlists when categories are created. I only want the user to be able to select one item from any jlist at a time. I also am retrieving information about that item selected, its position in a linked list for example. However with my code so far i get an error, but it doesn't seem to affect functionality, was wondering if anyone could help me work how why or how to fix it?
This is my code:
public void addListener(final JList jList, final int curListCount) {
jList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
jLabel2.setText("");
if (isSelected == true) {
isSelected = false;
jList.clearSelection();
jLabel2.setText("");
}
String selectedString = (jList.getSelectedValue().toString());
jLabel2.setText(selectedString);
int selectedIndex = jList.getSelectedIndex();
isSelected = true;
System.out.println("Array position is: " + curListCount);
System.out.println("LinkedList position is: " + selectedIndex);
}
});
}
And the error that comes up is:
'Array position is: 3 LinkedList position is: 0 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at smartChart.wbt$1.valueChanged(wbt.java:57)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1798)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1812)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415)
at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:576)
at javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:420)
at javax.swing.JList.clearSelection(JList.java:2045)
at smartChart.wbt$1.valueChanged(wbt.java:54)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1798)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1812)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:154)
at javax.swing.DefaultListSelectionModel.setValueIsAdjusting(DefaultListSelectionModel.java:685)
at javax.swing.JList.setValueIsAdjusting(JList.java:2142)
at javax.swing.plaf.basic.BasicListUI$Handler.mouseReleased(BasicListUI.java:2796)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)'
Any ideas? Thanks in advance.
String selectedString = (jList.getSelectedValue().toString());
Looks like this line throws the exception.
You clear selection in the same listener and it in turn calls selection change.
Try to move the value obtaining to be before jList.clearSelection().
Also I would introduce a flag to show the selection change from user's click or from your code which clears selection. Set the flag isMyCode to true (default false) and skip processing if it's true.

Categories