0

So what I'm trying to do is when I enter a primary id in a JTextField and click proceed, it will take that primary id and store it as a new record in another table. What happens when you click proceed is that it takes you to another app where the primary id is display from the previous app.

My problem here is that it won't let me display the text. My error says:

non-static variable pat_id cannot be referenced from a static context

How do I bypass this?

Here is my code:

private void proceedActionPerformed(java.awt.event.ActionEvent evt) {                                        
    try {

        String sql = "Insert into medicalRec (patient_id)" + " values (?)";

        pst = conn.prepareStatement(sql);
        pst.setInt(1, Integer.parseInt(pat_id.getText()));
        pst.execute();
        new medRec().setVisible(true);

        String sql2 = "select * from PATIENT where patient_id=?";
        pst = conn.prepareStatement(sql2);
        rs = pst.executeQuery();
        rs = pst.executeQuery();
        if (rs.next()) {

            String add0 = rs.getString("patient_id");
            medRec.pat_id.setText(add0);
        }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}
mfaerevaag
  • 730
  • 5
  • 29
  • What line number is your exception on? – cogsmos Jun 05 '13 at 16:54
  • Look at following question could solve your problem by your own. [non-static variable cannot be referenced from a static context](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Smit Jun 05 '13 at 16:55
  • 1
    Also what is this `new medRec().setVisible(true);` looks like you are creating an instance of a `medRec` and immediately setting it visible without keeping any reference to the record. – cogsmos Jun 05 '13 at 16:55
  • @cogsmos sorry it's medRec.pat_id.setText (add0); , thats where i get an error, also the new medRec().setVis.... takes you to the new app, which I am glad to say is functional – user1724891 Jun 05 '13 at 16:56
  • have you initialized your JTextField in the other class? For example: Class1{public static JTextField myText = new JTextField();} and then in the other class refer to it like Class1.myText.setText("something"); ? – Marcelo Tataje Jun 05 '13 at 17:03

1 Answers1

0

The confusion here seems to be with your medRec class. Without seeing all the code I can bet that medRec is a class name and pat_id is an instance variable that looks something like this:

public class medRec {
     ...
     public JTextField pat_id = new JTextField();
     ...
     public void setVisible(boolean b){ ... }
}

This means that you should get an instance of the class first, save it as a variable and manipulate setVisible and pat_id on that such as this:

medRec mr = new medRec();
mr.setVisible(true);
mr.pat_id.setText("abc");
cogsmos
  • 806
  • 6
  • 11