Click here to Skip to main content
15,308,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I am having a casting error this is the error....Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')

here is the method i am using
empID=(int)employees[selectedIndex-1][1];

 public void updateTable()
    {
         int selectedIndex=employeeCmb.getSelectedIndex();
         int empID;
         if (selectedIndex==0)
            empID =-1;
         else
            empID=(int)employees[selectedIndex-1][1];


         String adate=dateLbl.getText();
         
         q.connectToDatabase();
         empAppointments=q.getEmpAppointments_OA(empID,adate);
         int appCount=empAppointments.length;
         
         Object [][]empAppointments2 = new Object[appCount][4];
         for (int i =0; i < appCount; i++)
         {
             for (int j=0; j < 4;j++)
             {
                 empAppointments2[i][j]=empAppointments[i][j];
                 model=new DefaultTableModel(empAppointments2,colNames);
                 table.setModel(model);
                 q.disconnectFromDatabase(); 
          //       System.out.println(colNames);
             }
         }
    }
       
       
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==insBtn)
         {
              updateTable();
             
        }
        else if (e.getSource()==nextBtn)
        {
           l=l+86400000;
           d=new Date(l);
           s=sdf.format(d);
           dateLbl.setText(s);            
        }
            
        else if (e.getSource()==preBtn)
        {
            l=l-86400000;
            d=new Date(l);
            s=sdf.format(d);
            dateLbl.setText(s);  
        }
        
        else if (e.getSource()==delBtn)
        {
        int deleted=q.deleteOneClient(4);
        
        
        
        }
        
        else if(e.getSource()==updBtn)
        {
       int inserted=q.insertOneClient("ali","rahim","123 Some Rd","ddd@eee","3456777");
        
        
        }
    }

    public static void main(String[] args)
    {
       Queries q = new Queries();
       new BookingCard();
    }


}


What I have tried:

empID=(int)employees[selectedIndex-1][1];
this is where bluej shows as the error
Posted
Updated 13-May-21 10:10am
Comments
SeanChupas 13-May-21 15:00pm
   
So whatever is in employees[selectedIndex-1][1] is not an integer. Right? So, just fix that.
Member 14857105 13-May-21 15:40pm
   
i am not sure how to fix the error. Yes i am aware that there is a String that i am trying to convert to an Integer. Any ideas pls
SeanChupas 13-May-21 16:18pm
   
What is the value of employees[selectedIndex-1][1]
Richard MacCutchan 14-May-21 3:11am
   
You cannot use a cast in that way, you must use a conversion method.

1 solution

empID = Integer.parseInt(employees[selectedIndex-1][1]);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900