0

I'm getting problem with accessing variables with getter & setter on multiple classes. I looked up this one but I'm too confused.

I have 3 type users: Admin (Position No: 0 in mysql table), Manager (Position No: 1), Clerk (Position No:2).

I have SeeReportsAndFeedbacks class. I want to show all reports by selecting rows with position_no = 0 and 1 to admin and manager, 2 to clerk. It's already done with if statement.

So clerk can see only see reports that with position_no=2 manager can see only see reports that with position_no=0 and 1 admin can see only see reports that with position_no=0 and 1

Please help me. I'm stucked here for a long time. What are wrong with my getter setters?

If i set on Login_Form, and call get it shows correct in girisyap() function but if i call get in other class named SeeReportsAndFeedbacks it shows first initial value from Users () constructor instead of set value on girisyap() function on Login_Form.

tip value takes position_no from mysql db as string, new1 value is parsing (converting) string to int for if statement

screenshot

GIST

Users Class


    public class Users {

    private  int id;
    private String username;
    private String fullname;
    private String password;
    private String phone;
    private String gender;
    private byte[] image;
    private  int position_no;

public Users () {
    setPno(1); //firsst initialize
    //getFullname();
}
public Users (int uid ,String uname, String fname, String upassword, String uphone, String ugender, byte[] uimage, int pno){

this.id = uid;
this.username = uname;
this.fullname = fname;
this.password = upassword;
this.phone = uphone;
this.gender =ugender;
this.image =uimage;
this.position_no = pno;
}
public Users (int pno){

setPno(pno);
}

public int getPno(){        
    return position_no;
    }
public void setPno(int pno){
this.position_no = pno;       
    }}

SeeReportsAndFeedbacks class (i removed not-related funcs or some other gui things for the question.

public class SeeReportsAndFeedbacks { // extends javax.swing.JFrame

    //CLIENT client = new CLIENT();
    int new1 = 9999; //testing something
    int PositionNoGetiren;
    //sers loginf = new Users(0, null,null,null,null,null,null,new1);


    public SeeReportsAndFeedbacks() {
        //initComponents();

        Users loginf = new Users();

        PositionNoGetiren = loginf.getPno(); //gets initial value instead of set value on login_form
        System.out.println("Babakingg " + PositionNoGetiren);

        //int ananas = loginf.getPno();

        //fillFeedbackJTable(jTable2);


    }

        public void fillReportJTable(){//JTable table

        //loginf.setPno(2); it works if i manually set but it's useless
      //System.out.println("Loginfvalue in see reports: " + loginf.getPno() + loginf.getUsername());
        //new1 = loginf.getPno(); //not works shows 0

        //see.getNo();

        new1=PositionNoGetiren;

        String selectQuery = "SELECT * FROM `users` WHERE `position_no` = ?";


        if(new1==0){//admin

            selectQuery = "SELECT * FROM `reports`";

        }
        if(new1==1){//manager

            selectQuery = "SELECT * FROM `reports` WHERE `position_no` = 1";

        }
        if(new1==2){//clerk
            selectQuery = "SELECT * FROM `reports` WHERE `position_no` = 2";
        }
        //}


    }}

Login_Form

public class Login_Form {

    int positionNoGetiren;

    /**
     * Creates new form Login_Form
     */
    public Login_Form() {

        //initComponents();

        //positionNoGetiren = 9999;

    }

    private void girisyap() {                                                


                //I DELETED ALL DATABASE RELATED THINGS FOR QUESTION.
                //String tip = rs.getString("position_no"); //DETECTS CORRECTLY POSITION NO FROM DATABASE
                String tip = "IT'S rs.getString(\"position_no\")"; //for posting question
                System.out.println(tip);
                int new1 = Integer.parseInt(tip);

                //Users loginf = new Users(new1); //welcome yazisi icin
                Users loginf = new Users(); //ONLY WORKS IN THIS CLASS.

                loginf.setPno(new1); //set user type for reports class BUT IT'S NOT WORKING
                System.out.println("Loginf degeri login_formdaki: " + loginf.getPno());

                //THIS IF IS WORKING CORRECTLY.

                if(new1==0){ 

                //Admin form = new Admin();
                //form.setVisible(true);
                //form.pack();
                //form.setLocationRelativeTo(null);
              //  form.setExtendedState(JFrame.MAXIMIZED_BOTH);

                }
                if(new1==1){

                //Manager form = new Manager();
                //form.setVisible(true);
                //form.pack();
                //form.setLocationRelativeTo(null);

                }
                if(new1==2){

                //Clerk form = new Clerk();
                //form.setVisible(true);
                //form.pack();
                //form.setLocationRelativeTo(null);
               // form.setExtendedState(JFrame.MAXIMIZED_BOTH);
                }

                //this.dispose();




        }

    private void jButton_LoginActionPerformed(java.awt.event.ActionEvent evt) {                                              
        girisyap();} 

    }
tosunkaya
  • 71
  • 1
  • 11

1 Answers1

0

There is nothing wrong with your getters and setters.

This is most likely an issue with parsing the value of TIP, that could not be parsed as an INT, maybe its a float with a weird value of like 2.00000004, or simply null. Try writing a test or log the value which your query return and check if this is the value you are looking for.

Dirk Heijnen
  • 51
  • 1
  • 8
  • there is no problem with tip. if statement in the login_form is working correctly. if admins logins, shows admin panel, manager logins shows manager panel, clerk logins shows, clerk panel. :( if i write loginf.setPno(9); instead of loginf.setPno(new1); it still shows 1 (the initial value on the Users constructor) :( :( – tosunkaya Dec 30 '19 at 21:47