mercredi 18 mars 2015

NullPointerException in Java JComboBox


Vote count:

0




In trying to run my code i get NullPointerException error.I think the error is due to comboBox4 getselectedItem(),if i run the code using two comboxes it works fine.Any suggestion would be helpful.



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class Test extends JPanel
{
JTextField field11,field12,field13;
JComboBox comboBox2,comboBox3,comboBox4;

public Test() {

JPanel buttonPanel = new JPanel();
add(buttonPanel);
buttonPanel.setLayout(new GridLayout(0, 3, 5, 5));

field11 = new JTextField(10);
buttonPanel.add(field11);

field12 = new JTextField(10);
buttonPanel.add(field12);

field13 = new JTextField(10);
buttonPanel.add(field13);

comboBox2 = new JComboBox();
comboBox2.addItem("1");
comboBox2.addItem("2");
comboBox2.addItem("3");
buttonPanel.add(comboBox2);

comboBox2.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
change();
}
}
});

comboBox3 = new JComboBox();
comboBox3.setEditable(true);
buttonPanel.add(comboBox3);
comboBox3.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
change();
}
}
});

comboBox4 = new JComboBox();
comboBox4.setEditable(true);
buttonPanel.add(comboBox4);
comboBox4.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
change();
}
}
});


JComboBox[] comboBoxs = new JComboBox[3];
comboBoxs[0] = comboBox2;
comboBoxs[1] = comboBox3;
comboBoxs[2] = comboBox4;


ArrayList<String> list = new ArrayList<String>();

final StringBuilder temp = new StringBuilder();

try{
InputStream ips=new FileInputStream("test2.txt");
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);

String line;
//boolean found = false;

while ((line=br.readLine())!=null) {
String[] s = line.split(" ");
list.add(s[0]);
list.add(s[1]);
list.add(s[2]);
}
}
catch (Exception e){
e.printStackTrace();
}

for(int i = 0; i < comboBoxs.length; i++) {
comboBoxs[i].setSelectedItem(list.get(i));
}
}

public void change() {
Object text = comboBox2.getSelectedItem();
if(text.toString().equals("1")) {
field11.setText("10");
String cb3a = ((String)comboBox3.getSelectedItem());
double cb3b = Double.valueOf(cb3a);
String cb3aa = field11.getText();
double cb3bb = Double.parseDouble(cb3aa);
double cb3c = (cb3b*cb3bb);
String cb3d = String.valueOf(cb3c);
field12.setText(cb3d);

String cb4a = ((String)comboBox4.getSelectedItem());
double cb4b = Double.valueOf(cb4a);
double cb4c = (cb4b*cb3bb);
String cb4d = String.valueOf(cb4c);
field13.setText(cb4d);
}
else if(text.toString().equals("2")) {
field11.setText("20");
String cb33a = ((String)comboBox3.getSelectedItem());
double cb33b = Double.valueOf(cb33a);
String cb33aa = field11.getText();
double cb33bb = Double.parseDouble(cb33aa);
double cb33c = (cb33b*cb33bb);
String cb33d = String.valueOf(cb33c);
field12.setText(cb33d);
}
else if(text.toString().equals("3")) {
field11.setText("30");
String cb333a = ((String)comboBox3.getSelectedItem());
double cb333b = Double.valueOf(cb333a);
String cb333aa = field11.getText();
double cb333bb = Double.parseDouble(cb333aa);
double cb333c = (cb333b*cb333bb);
String cb333d = String.valueOf(cb333c);
field12.setText(cb333d);

}
}

public static void main(String[] args) {
Test app = new Test();
JFrame m = new JFrame("D");
m.getContentPane().add(app);
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.pack();
m.setVisible(true);
}
}


test2.txt: 1 2 4



asked 51 secs ago







NullPointerException in Java JComboBox

Aucun commentaire:

Enregistrer un commentaire