Results 1 to 12 of 12
- 11-25-2011, 02:18 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Getting the String From JComboBox
Ok so I used a JComboBox to give a choice of premade security questions.
I then want to put the selected item into a String array, but I can't seem to get the text from the selected Item. The closest I got to running the program gives me a nullPointerException.
Any Suggestions??
- 11-25-2011, 02:28 PM #2
Re: Getting the String From JComboBox
Post your code and the full text of the error message.Any Suggestions??
- 11-25-2011, 02:34 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Getting the String From JComboBox
This is the error message
java.lang.NullPointerException
at createGUI.<init>(createGUI.java:130)
at mainMenu.<init>(mainMenu.java:46)
at mainMenu.main(mainMenu.java:37)
and this is the Action Event code
secureA being the JComboBox and secretAnswer[x] being the arrayJava Code:public void actionPerformed(ActionEvent event) { String menuName; menuName = event.getActionCommand(); if( event.getSource() == secureA) { JComboBox secureA = (JComboBox)event.getSource(); secretAnswer[x] = (String)secureA.getSelectedItem(); System.out.println(secretAnswer[x]); } }
- 11-25-2011, 02:40 PM #4
Re: Getting the String From JComboBox
The error says there is a variable at line 130 with a null value.java.lang.NullPointerException
at createGUI.<init>(createGUI.java:130)
Look at the code on line 130 and see what variable has a null value. Back track in your code to see why the variable does not have a valid value.
If you can not see what variable is null, add a println just before line 130 and print out the values of all the variables used on line 130.
- 11-25-2011, 02:45 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Getting the String From JComboBox
the class createGUI does not process any math whatsoever. It only creates and displays (or not) all my objects.
Line 130 is a comment. The line just before is setting the font of a button (which works perfectly).
The class where the action performed is located is not even listed in the errors.
Line 46 of the mainMenu is a '{' and line 37 is 'public static void main(String[] args) 'Last edited by jiffi; 11-25-2011 at 02:47 PM.
- 11-25-2011, 04:19 PM #6
Re: Getting the String From JComboBox
The error message says the error occurred at line 130.
If you have changed the source, then the line with the error could have moved.
Are you sure you are executing the current version of the program?
You need to find the null variable and give it a value to keep from getting a NullPointerException.
I don't see where the error you posted has anything to do with the actionPerformed method.
- 11-25-2011, 10:56 PM #7
Re: Getting the String From JComboBox
And your class names should start with uppercase letters.
Code Conventions for the Java(TM) Programming Language: Contents
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-26-2011, 03:45 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Getting the String From JComboBox
All I know is if I remove the action performed For the JComboBox I don't get the error anymore. And no I haven't changed the source code between the time I got the error and the time I checked the lines.
I didn't know that. ThanksAnd your class names should start with uppercase letters.
-
Re: Getting the String From JComboBox
Still a bit hard to guess what is wrong. You may need to post some code (an SSCCE would be best) and indicate the location of the NPE.
- 11-26-2011, 05:28 AM #10
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Getting the String From JComboBox
Here is the code. I don't know what NPE is though
Java Code:import java.awt.*; import javax.swing.*; import java.lang.*; import java.util.*; import java.text.*; import java.awt.event.*; import java.io.*; public class JavaForums extends JFrame implements ActionListener { //***********************// //***Positions & Sizes***// //***********************// public static final int FRAME_WIDTH = 700; public static final int FRAME_HEIGHT = 700; public static final int FRAME_X_ORIGIN = 640; public static final int FRAME_Y_ORIGIN = 140; public static final int buttonW = 125; public static final int buttonH = 58; //*********************// //*******Objects*******// //*********************// public JButton proceedButton; public Container contentPane; public JLabel addCardMessage; public JLabel cardNameMess; public JLabel passcodeMess; public JLabel secureQMess; public JLabel secureAMess; public JLabel cardNameErr; public JLabel passcodeErr; public JLabel passcodeErr2; public JLabel secureAErr; public JTextField cardName; public JPasswordField passcode; public JPasswordField passcodeCheck; public JTextField secureA; public JComboBox secureQ; //**********************// //*********Fonts********// //**********************// public Font mainFont; public Font startFont; public Font buttonFont; public Font cardFont; public Font cardErrFont; //**********************// //***Public Variables***// //**********************// String [] secureQChoice = {"What is the name of your first pet", "What is the middle name of your mother/father", "What hour were you born on?", "What is the name of your favorite teacher?", "What is your favorite TV series/movie?", "If you could change your name, what would it be?"}; public int nTotal = 5; public int x = 0; public String [] creditCard = new String[nTotal];//credit cards public String [] secretQuestion = new String[nTotal];//secret question public String [] secretAnswer = new String [nTotal];//secret answer public String [] passCode = new String [nTotal];//passcode public int cardAdded = 0;//switch public static void main (String [] args) { JavaForums javaForums = new JavaForums(); javaForums.setVisible(true); } public JavaForums() { setTitle ("Main Menu"); setSize (FRAME_WIDTH, FRAME_HEIGHT); setLocationRelativeTo(null); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); contentPane = getContentPane(); contentPane.setLayout(null); contentPane.setBackground(Color.red); mainFont = new Font ("Comic Sans MS", Font.BOLD, 20); startFont = new Font("Comic Sans MS", Font.BOLD, 30); buttonFont = new Font("Comic Sans MS", Font.BOLD, 13); cardFont = new Font("Comic Sans MS", Font.BOLD,15); cardErrFont = new Font("Comic Sans MS",Font.PLAIN,13); addCardMessage = new JLabel(); addCardMessage.setText("<html><center>Please Fill in all the Forms and Click on 'Proceed'.</html>"); addCardMessage.setBounds(125,100,450,50); addCardMessage.setFont(mainFont); addCardMessage.setForeground(Color.white); contentPane.add(addCardMessage); cardNameMess = new JLabel(); cardNameMess.setText("Choose a Name for your Credit Card"); cardNameMess.setBounds(150,155,400,25); cardNameMess.setFont(cardFont); contentPane.add(cardNameMess); cardNameErr = new JLabel(); cardNameErr.setText("Invalid Card Name. Try Again"); cardNameErr.setBounds(150,175,400,25); cardNameErr.setVisible(false); cardNameErr.setFont(cardErrFont); contentPane.add(cardNameErr); cardName = new JTextField(); cardName.setBounds(150,205,400,25); cardName.addActionListener(this); contentPane.add(cardName); passcodeMess = new JLabel(); passcodeMess.setText("Choose a 4 Digits Passcode, and then retype it"); passcodeMess.setBounds(150,240,400,25); passcodeMess.setFont(cardFont); contentPane.add(passcodeMess); passcodeErr = new JLabel(); passcodeErr.setText("Invalid Passcode. Try Again"); passcodeErr.setBounds(150,260,400,25); passcodeErr.setVisible(false); passcodeErr.setFont(cardErrFont); contentPane.add(passcodeErr); passcodeErr2 = new JLabel(); passcodeErr2.setText("Passcodes Did not Match"); passcodeErr2.setBounds(150,260,400,25); passcodeErr2.setVisible(false); passcodeErr2.setFont(cardErrFont); contentPane.add(passcodeErr2); passcode = new JPasswordField(); passcode.setBounds(150,290,400,25); passcode.addActionListener(this); contentPane.add(passcode); passcodeCheck = new JPasswordField(); passcodeCheck.setBounds(150,320,400,25); passcodeCheck.addActionListener(this); contentPane.add(passcodeCheck); secureQMess = new JLabel(); secureQMess.setText("Choose a Security Question"); secureQMess.setBounds(150,360,400,25); secureQMess.setFont(cardFont); contentPane.add(secureQMess); secureQ = new JComboBox(secureQChoice); secureQ.setBounds(150,390,400,25); secureQ.setEditable(false); secureQ.addActionListener(this); contentPane.add(secureQ); secureAMess = new JLabel(); secureAMess.setText("Answer Your Secret Question"); secureAMess.setBounds(150,425,400,25); secureAMess.setFont(cardFont); contentPane.add(secureAMess); secureA = new JTextField(); secureA.setBounds(150,455,400,25); secureA.addActionListener(this); contentPane.add(secureA); proceedButton = new JButton("Proceed"); proceedButton.setBounds(220,500,buttonW,buttonH); proceedButton.addActionListener(this); proceedButton.setFont(buttonFont); contentPane.add(proceedButton); } public void actionPerformed(ActionEvent event) // Actions { String menuName; menuName = event.getActionCommand(); if( event.getSource() == secureA) { JComboBox secureA = (JComboBox)event.getSource(); secretAnswer[x] = (String)secureA.getSelectedItem(); System.out.println(secretAnswer[x]); } if (event.getSource() instanceof JButton) { processButtonAction(menuName); } } private void processButtonAction(String buttonText) { if (buttonText.equals("Proceed")) { int correctCode = 0; int correctName = 0; int check = 1; String checkCode = passcode.getText(); String checkCode2 = passcodeCheck.getText(); String checkName = cardName.getText(); String pattern = "[0-9]{4}";//numbers 0-9, only numbers, only 4 numbers if (! checkCode.matches(pattern)) { passcodeErr.setVisible(true); passcodeErr2.setVisible(false); correctCode = 0; } else if (! checkCode.equals(checkCode2)) { correctCode = 2; passcodeErr.setVisible(false); passcodeErr2.setVisible(true); } else if(checkCode.matches(pattern)) { passcodeErr.setVisible(false); passcodeErr2.setVisible(false); correctCode = 1; } if (check==1&&correctCode==1) { creditCard[x] = checkName; passCode[x] = checkCode; secretAnswer[x] = secureA.getText(); System.out.println(secretQuestion[x]); x++; cardAdded=1; try{ File outFile = new File("Check2.txt"); FileOutputStream outFileStream = new FileOutputStream(outFile); PrintWriter outStream = new PrintWriter(outFileStream); outStream.println(cardAdded); outStream.close(); } catch(IOException e) { } addCardMessage.setVisible(false); cardName.setVisible(false); cardNameMess.setVisible(false); passcode.setVisible(false); passcodeMess.setVisible(false); secureQ.setVisible(false); secureQMess.setVisible(false); secureAMess.setVisible(false); secureA.setVisible(false); proceedButton.setVisible(false); passcodeCheck.setVisible(false); passcodeErr2.setVisible(false); passcodeErr.setVisible(false); cardName.setText(""); passcode.setText(""); passcodeCheck.setText(""); secureA.setText(""); } } } }
- 11-26-2011, 08:21 AM #11
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Getting the String From JComboBox
I realized the NPE comes from my other code, not this one.
I still can't figure out how to get the user's selection to be put into an array though.
- 11-26-2011, 12:56 PM #12
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Similar Threads
-
JComboBox
By Java_25 in forum New To JavaReplies: 10Last Post: 04-20-2011, 06:39 PM -
Comparing JComboBox with string?
By vahshir in forum New To JavaReplies: 2Last Post: 10-10-2010, 07:39 AM -
Activate JComboBox 1 when object is selected in JComboBox 2...
By bahumbaba in forum AWT / SwingReplies: 2Last Post: 12-10-2009, 01:58 PM -
Jcombobox not liking String input for options :|
By skatefreak in forum New To JavaReplies: 4Last Post: 05-11-2009, 12:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks