i'm using java3D and want to pass the ID of clicked items to my class. however it doesn't succeed. it can print the ID and also can pass it to the string text. but it says nullPointerException in the line game.btClicked(text); how can i pass the ID to my class?
public class GUI extends JFrame implements ActionListener, MouseListener {
private Game game;
private String text;
public GUI(Game game /*...*/) {
this.game = game;
//.......
}
//.......
public void mouseClicked(MouseEvent e) {
pickCanvas.setShapeLocation(e);
Primitive pickedShape = null;
PickResult result = pickCanvas.pickClosest();
if (result != null) {
pickedShape = (Primitive) result.getNode(PickResult.PRIMITIVE);
}
//actions to be carried out when object is clicked
if (pickedShape != null) {
System.out.println("clicked: " + pickedShape.getName());
String text = pickedShape.getName();
System.out.println(text);
game.btClicked(text);
} else {
}
}
}
public class Game {
public Game(){
//.......
}
public void btClicked(String text) {
spielfeld.disableAll();
int buttonNr = Integer.parseInt(text);
System.out.println("step: "+step+ " nr: "+buttonNr);
// .......
}
}
the following is the stack trace:
clicked: 38
38
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at spiel.Game.btClicked(Game.java:403)
at spiel.SpielGUI.mouseClicked(GUI.java:552)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
that is quite a lot, but the most is due to other problems, I'm coding a new GUI for the game so there are still some code to be changed. so just look at the first lines.
"GUI.java:552" refers to "game.btClicked(text);"
"Game.java:403" refer to "spielfeld.disableAll();" but I'm sure this line is OK.
You don't have a constructor in the code your provided. You have a method that looks very much like a constructor. I'm assuming Game isn't getting initialised.
A constructor doesn't define a return type. In your case you have
public void GUI(Game game ....)
When you actually need
public GUI(Game game ....)
Here's an example:
public class GUI {
boolean b = false;
public static void main(String[] args) {
GUI g = new GUI();
System.out.println(g.b);
}
public void GUI() {
b = true;
}
}
Provides output:
run:
false
BUILD SUCCESSFUL (total time: 2 seconds)
Whereas the correct constructor would be:
public class GUI {
boolean b = false;
public static void main(String[] args) {
GUI g = new GUI();
System.out.println(g.b);
}
public GUI() {
b = true;
}
}
Which gives:
run:
true
BUILD SUCCESSFUL (total time: 1 second)
Related
I am making a simple gui program in java. When i click run it gives me an error that looks like this:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at javax.swing.JLayeredPane.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JRootPane.setContentPane(Unknown Source)
at javax.swing.JFrame.setContentPane(Unknown Source)
at main.cool(main.java:31)
at main$1.run(main.java:43)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
and here is my code:
import javax.swing.*;
import java.awt.Container;
import java.awt.event.*;
public class main extends JFrame implements ActionListener {
protected JButton click, fun;
public main()
{
click = new JButton("Click");
click.setActionCommand("click");
click.addActionListener(this);
add(click);
click.setSize(16, 16);
fun = new JButton("wow");
fun.setActionCommand("wow");
fun.addActionListener(this);
add(fun);
}
public static void cool()
{
JFrame frame = new JFrame("TEST!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main main = new main();
frame.setContentPane(main);
frame.setSize(128, 128);
frame.setVisible(true);
frame.setResizable(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
cool();
}
});
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if("click".equals(e.getActionCommand()))
{
System.out.println("oh right");
} else if ("wow".equals(e.getActionCommand()))
{
System.out.println("hi");
}
}
}
I believe the bug may be in the cool() method; with the setContentPane line. but not sure. Can anyone please help me.
Yes; the bug is triggered in the frame.setContentPanel() in your cool() method in your main class. Your main class should extend JPanel instead of extend JFrame.
Aside: avoid declaring local variables which match class names; and, avoid declaring class names which are the same as standard method names...so, rename your main class as Main (if you must, but try to be more descriptive...perhaps Application), and make your local variable name something other than main...perhaps m or application.
Your class main extends JFrame instead of JPanel, so you are calling frame.setContentPane() on another JFrame, and it doesn't make sense to have a JFrame inside a JFrame. Change the superclass to JPanel and the error will go away
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.sql.*;
import java.net.*;
public class connection {
JTextField textfeild;
JButton button;
String text;
Socket sock;
PrintWriter writer;
JButton button1;
public static void main(String[] args) {
connection user1 = new connection();
user1.go();
}//main method close
public void go() {
JFrame frame12 = new JFrame();
JPanel centerpanel12 = new JPanel();
centerpanel12.setLayout(new BoxLayout(centerpanel12, BoxLayout.Y_AXIS));
textfeild = new JTextField(20);
centerpanel12.add(textfeild);
//textfeild.addActionListener(new textfeildlitner());
frame12.add(centerpanel12);
button = new JButton("Click Me");
centerpanel12.add(button);
button.addActionListener(new buttonlitner());
button1 = new JButton("DataDisplay");
centerpanel12.add(button1);
button1.addActionListener(new buttonlitner1());
frame12.getContentPane().add(BorderLayout.CENTER, centerpanel12);
frame12.pack();
frame12.setVisible(true);
frame12.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//go method close
/*class textfeildlitner implements ActionListener{
public void actionPerformed(ActionEvent ev){
}
}//inner class textfeildlitner close*/
class buttonlitner implements ActionListener {
public void actionPerformed(ActionEvent ev) {
button.setText("I AM Clicked");
String name = textfeild.getText();
System.out.println(name);
textfeild.setText("");
textfeild.requestFocus();
}//method close
}//inner class close
class buttonlitner1 implements ActionListener {
void connection() {
try {
String user = "SQlUI";
String pass = "123456";
String db = "jdbc:sqlserver://localhost:1234;" + ";databaseName=SQlUI";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(db, user, pass);
Statement s1 = con.createStatement();
ResultSet r1 = s1.executeQuery("select * from Table_1");
String[] result = new String[20];
if (r1 != null) {
while (r1.next()) {
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result.length; j++) {
result[j] = r1.getString(i);
System.out.println(result[j]);
break;
}//for j close
}//for i close
}//if closeclose
}//try close
} catch (Exception ex) {
ex.printStackTrace();
}//catch close
}//connection() close
public void actionPerformed(ActionEvent ev) {
button1.setText("Processing");
new buttonlitner1().connection();
}//method close
}//inner class close
}//outer class close
And the exception which i am getting for this code-
com.microsoft.sqlserver.jdbc.SQLServerException: The index 0 is out of range.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyValidColumnIndex(SQLServerResultSet.java:531)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(SQLServerResultSet.java:2049)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:2082)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:2067)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getString(SQLServerResultSet.java:2392)
at connection$buttonlitner1.connection(connection.java:76)
at connection$buttonlitner1.actionPerformed(connection.java:89)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
When i am changing the value of i from 0 to 1 then it displays result for 1st row but again it throws exception saying that the index 3 is out of range.
I have posted the same code and after making the changes (as suggested by the experts on stackoverflow) it gave me a new type of exception.
I am putting the link of the previous question-Getting Checked Exception while running a SQL Program and the question was Getting Checked Exception while running a SQL Program
In SQL (unlike generally in Java) evrything is indexed starting from 1 - both rows and columns.
Why do you have 2 loops inside your r1.next() loop?
Think what your code
for (int j = 0; j < result.length; j++) {
result[j] = r1.getString(i);
System.out.println(result[j]);
break;
}
actually does.
I am trying to design a three cascade JComboBox in JAVA:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class ThreeCascadeJComboBox {
private JComboBox combo1;
private JComboBox combo2;
private JComboBox combo3;
public static void main(String[] args) {
new ThreeCascadeJComboBox();
}
public ThreeCascadeJComboBox() {
JFrame v = new JFrame();
v.getContentPane().setLayout(new FlowLayout());
combo1 = new JComboBox();
loadCombo1();
combo1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loadCombo2((String) combo1.getSelectedItem());
}
});
combo2 = new JComboBox();
loadCombo2((String) combo1.getSelectedItem());
combo2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loadCombo3((String) combo2.getSelectedItem());
}
});
combo3 = new JComboBox();
loadCombo3((String) combo2.getSelectedItem());
v.getContentPane().add(combo1);
v.getContentPane().add(combo2);
v.getContentPane().add(combo3);
v.pack();
v.setVisible(true);
v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void loadCombo1() {
combo1.addItem("letters");
combo1.addItem("numbers");
}
private void loadCombo2(String seleccionEnCombo1) {
combo2.removeAllItems();
if (seleccionEnCombo1.equals("letters")) {
combo2.addItem("A");
combo2.addItem("B");
combo2.addItem("C");
} else if (seleccionEnCombo1.equals("numbers")) {
combo2.addItem("1");
combo2.addItem("2");
combo2.addItem("3");
}
}
private void loadCombo3(String seleccionEnCombo2) {
combo3.removeAllItems();
if (seleccionEnCombo2.equals("A")) {
combo3.addItem("A-1");
combo3.addItem("A-2");
combo3.addItem("A-3");
} else if (seleccionEnCombo2.equals("B")) {
combo3.addItem("B-1");
combo3.addItem("B-2");
combo3.addItem("B-3");
} else if (seleccionEnCombo2.equals("C")) {
combo3.addItem("C-1");
combo3.addItem("C-2");
combo3.addItem("C-3");
} else if (seleccionEnCombo2.equals("1")) {
combo3.addItem("1-a");
combo3.addItem("1-b");
combo3.addItem("1-c");
} else if (seleccionEnCombo2.equals("2")) {
combo3.addItem("2-a");
combo3.addItem("2-b");
combo3.addItem("2-c");
} else if (seleccionEnCombo2.equals("3")) {
combo3.addItem("3-a");
combo3.addItem("3-b");
combo3.addItem("3-c");
}
}
}
But I get the next Exception when I select the numbers value in the jcombo1:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at es.mycompany.MyView.ThreeCascadeJComboBox.loadCombo3(ThreeCascadeJComboBox.java:78)
at es.mycompany.MyView.ThreeCascadeJComboBox.access$3(ThreeCascadeJComboBox.java:76)
at es.mycompany.MyView.ThreeCascadeJComboBox$2.actionPerformed(ThreeCascadeJComboBox.java:40)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.JComboBox.intervalRemoved(Unknown Source)
at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
at javax.swing.DefaultComboBoxModel.removeAllElements(Unknown Source)
at javax.swing.JComboBox.removeAllItems(Unknown Source)
at es.mycompany.MyView.ThreeCascadeJComboBox.loadCombo2(ThreeCascadeJComboBox.java:63)
at es.mycompany.MyView.ThreeCascadeJComboBox.access$1(ThreeCascadeJComboBox.java:62)
at es.mycompany.MyView.ThreeCascadeJComboBox$1.actionPerformed(ThreeCascadeJComboBox.java:30)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
The exception is thrown because at that point seleccionEnCombo2 is null.
You could add a check for null in the combo2 ActionListener and it will work fine:
if (combo2.getSelectedItem() != null) {
loadCombo3((String) combo2.getSelectedItem());
}
The problem is that the ActionListener for combo1 is triggering an ActionEvent for combo2 which will not have any selected item (as it is empty). You could add a check:
if (combo2.getSelectedItem() != null) {
loadCombo3((String) combo2.getSelectedItem());
}
As the other posts have mentioned, the selected value for the combo is null in some cases. This is occurring because you probably do not realize the ActionListener for combo2 is getting called twice. The first time it gets called is during the call to removeAllElements. This is where the null value is coming from. The second time is what you are assuming in your code to be the only call--this is in response to both population of the combo box as well as user interaction.
When you load the second combo box, that fires the action event for that box (because an action has taken place [actions are not limited to selection]. The 2nd combo box's actionPerformed attempts to load the 3rd combo box based on the 2nd combo box's selection, and there isn't any. That's your null pointer, the non-existent selection from the second combo box.
I have a java program that creates a JFrame like this:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui.setFrame(new gui(), 1000, 300);
}
});
I also have a class (gui.java) that implements setFrame:
public static void setFrame(final JFrame frame, final int width, final int height) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
f1=frame;
frame.setTitle("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
});
}
If the user tries to click Submit (a button I created) and the fields in the JFrame are not filled in then it throws an error. The code for the error message is:
submit.addMouseListener(new MouseListener(){
#Override
public void mouseClicked(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {
//check to make sure all values filled in
if(chooser.getSelectedFile().toString()!=null&&saveChooser.getSelectedFile().toString()!=null)
parseFile.readFile(chooser.getSelectedFile(),saveChooser.getSelectedFile(),startSpanText.getText(),(String)col2.getSelectedItem(),(String)col3.getSelectedItem(),(String)col4.getSelectedItem(),(String)col5.getSelectedItem(),(String)col6.getSelectedItem());
else
JOptionPane.showMessageDialog(f1,"Bad");
}
});
//Note: f1 is a static version of the frame I initially received
The error I get is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gui$3.mouseReleased(gui.java:133)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
What did I do wrong?
One of the variables is null. It could be:
chooser
chooser.getSelectedFile()
saveChooser
saveChooser.getSelectedFile()
parseFile
startSpanText
col2 to col6
Use the line number in the stack trace, and a debugger or traces in the code to know which one. My guess would be one of the selected files, since it makes no sense to call
chooser.getSelectedFile().toString() != null
Either there is no selected file, and it throws an NPE because getSelectedFile() returns null, or there is one, and its toString() will never be null.
Also, you shouldn't use a mouse listener to do something when a button is pressed. That's what an ActionListener is for. It will be simpler, and also work when the user presses the button with its keyboard.
Your exception message should point you to the answer. One of the items you are using in your mouseReleased method is null:
#Override
public void mouseReleased(MouseEvent e) {
//check to make sure all values filled in
if(chooser.getSelectedFile().toString()!=null &&
saveChooser.getSelectedFile().toString()!=null)
parseFile.readFile(chooser.getSelectedFile(), saveChooser.getSelectedFile(),
startSpanText.getText(), (String)col2.getSelectedItem(),
(String)col3.getSelectedItem(), (String)col4.getSelectedItem(),
(String)col5.getSelectedItem(), (String)col6.getSelectedItem());
else
JOptionPane.showMessageDialog(f1,"Bad");
}
Use a debugger to inspect which variables are set. Use the line number in the exception message to help you.
I have as a task to simulate how queues evolve in a supermarket and I have to use timers to output whenever a client enters or exists the store. In the supermarket I have 3 queues that should process customers. At each client, they should output in my GUI the arrival time and the leaving time. Can I use the same timer for each queue? (they should not start outputting simultaneously or for the same period of time). I tried to pass the timer as a parameter in the event class but I get a Null Pointer exception. Help pls.
public void simulationPrep (Queue q,JPanel p,int time,Timer t, int selection)
{
TimeClass tc;
int queueCapacity=0;
float queueProcTime=0;
float tempSTMin, float tempSTMax;
tempSTMin=getServiceTimeMin();
tempSTMax=getServiceTimeMax();
queueCapacity = 10 + (int)( Math.random()*20);
queueProcTime = tempSTMin + (float)(Math.random()*tempSTMax);
q1.setCapacity(queueCapacity);
q1.setProcessingTime(queueProcTime);
tc = new TimeClass((int)(queueProcTime/queueCapacity),p,selection);
t = new Timer(time, tc);
t.start();
}
public void simulate()
{
if(getSelection()>=1)
{
simulationPrep(q1,p21,1000,timer1,1);
if(getSelection()>=2)
{
simulationPrep(q2,p22,1500,timer2,2);
if(getSelection()==3)
{
simulationPrep(q3,p23,1700,timer3,3);
}
}
}
}
public class TimeClass implements ActionListener
{
int counter;
JPanel p;
int selection;
public TimeClass(int counter,JPanel p,int selection)
{
this.counter = counter;
this.p = p;
this.selection = selection;
}
public void actionPerformed(ActionEvent e)
{
counter--;
if(counter>=1)
{
p.add(new JLabel("Hi "+counter));
p.updateUI();
}
if(counter<1)
{
p.add(new JLabel("Done"));
p.updateUI();
timer1.stop();
timer2.stop();
timer3.stop();
}
}
}
If one does not understand the code please ask, I'll be more specific, but this is my first post here and I'm kind of new to Java.
This is what I get in the output window :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at simulation.Simulation$TimeClass.actionPerformed(Simulation.java:229)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at simulation.Simulation$TimeClass.actionPerformed(Simulation.java:229)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Line 229 of your Simulation.java file is causing a NullPointerException. Specifically it is in the actionPerformed(...) method of your TimeClass inner class.
Go to that line, and there you'll have your problem.
I suspect it is one of these 3 lines:
timer1.stop();
timer2.stop();
timer3.stop();
One of these timer variables is probably null.
A quick fix (although probably not addressing the real problem) is this:
if (timer1 != null) {
timer1.stop();
}
and so on with the other two timer variables.