HEre is my checkout.java

index LCval = new index();

     final String LCnum = LCval.getVal();

    JButton btnNewButton_1 = new JButton("Finish");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
                   int bal = 0;
                 String connectionURL = "jdbc:sqlserver://ROHAN\\SQLEXPRESS;Database=sys;user=rohan;password=rurouni;";
                 Connection con = DriverManager.getConnection(connectionURL);
                PreparedStatement pst =null;
                ResultSet rs = null;
                    //Statement st=con.createStatement();

                    String sql="Select Balance  From loyaltycard where LCnum='"+LCnum+"' ";

                  bal = rs.getInt("Balance");
                  JOptionPane.showMessageDialog(null,"Payment Successful!\n Your current balance is:"+bal,"Client",JOptionPane.INFORMATION_MESSAGE);
                    new index().setVisible(true);
              catch(Exception e1){e1.printStackTrace();}


Here is my index.java

String value1;
    btnNewButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         String Cusname = null;

                             String connectionURL = "jdbc:sqlserver://ROHAN\\SQLEXPRESS;Database=sys;user=rohan;password=rurouni;";
                             Connection con = DriverManager.getConnection(connectionURL);
                                Statement st=con.createStatement();
                                ResultSet rs=st.executeQuery("select Name from loyaltycard where LCnum='"+value1+"'");
                                int count=0;
                                    Cusname = rs.getString("Name");

                               if(value1.equals("")) {
                               JOptionPane.showMessageDialog(null,"Enter Loyalty Card Number","Error",JOptionPane.ERROR_MESSAGE);
                               else if(count>0){

                              JOptionPane.showMessageDialog(null,"Login Successful \n"+Name,"Welcome",JOptionPane.PLAIN_MESSAGE);

                              new myitems().setVisible(true);

                               JOptionPane.showMessageDialog(null,"Invalid Loyalty Card Number","Error",JOptionPane.ERROR_MESSAGE);

                               catch(Exception e1){e1.printStackTrace();}



            public String getVal()

                 return value1;

*i want to get the value inputted in the textfield of index.java to checkout.java when i press the button on checkout.java i get this error.

com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the varchar value 'null' to data type int.*

3 Years
Discussion Span
Last Post by JamesCherrill

that is because a primitive can not be null.
null is when you have an Object that is not yet instantiated. you'll need to verify first whether or not you have null there, before trying to parse it to an int, and provide a default value or throw an Exception.


In Checkout you create a new Index then immediately get LCNum from it. At that point the user has had no chance to interact with that Index, so its value1 has never been set, so it's null, which is why you get that SQlerror (phew!).

The fix for this lies in the code that manages these separate parts - which wasn't posted, so it's hard to say any more, but basically you need to get a reference to the "real" Index instance, and pass that to Checkout so it can query value1 correctly.


this is an example of throwing an Exception.

if ( value == null ) {
throw new NullPointerException("No result found");
return value; // parsed to int

here's an example of a default value, but then you must have a limited nr of possible returns. for instance, if you expect a value between 0 and 10 (for instance points on a test, a minimum is always 0, a maximum is always 10)

if ( value == null){
  return -1;
return value; // parsed to int

of course, in the calling code, you need to write the logic to handle a -1 as result.

as for the first example (Exception) you'll need to add some exception handling.

but if you don't know how to do this, working with an UI is a bit too far for you, you don't control the basics enough yet to take a leap and pass some of the basic chapters.


I think the eror handling exception is not the real problem here - it's addressing the symptom, not the cause. The String is null becuase of an error in the code structure (see my previous post) - fix that and the exception will never be thrown


what's the proper way of get the value from textfield and retrieve it to the other class? cause i don't quite understand on how am i gonna solve my problem. Im still new thanks for the help advance.


Like I said - the Checkout needs a reference to the real Index so it can call its getVal() method. Normally that happens in the main code that opens all those windows, maybe something a bit like this:

Index theIndex = new Index(); 
// then when the checkout window is opened
// pass theIndex as a parameter to its constructor
Checkout theCheckout = new Checkout(theIndex);

... but the details of your will of course be different


already solved the problem i added static from the public String getVal() and it solved the problem


Making stuff static will solve this kind of problem in the special case where there is only one instance of the relevant classes, which is presumably the case here. Just be aware that in more complex (real) situations you will have multiple windows and multiple values, so the static approach won't work for those. Anyway, well solved!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.