Results 1 to 5 of 5
  1. #1
    MarMer is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default What error is it ?

    Actually I want my application to read the data from user and store it in a file. And the data can be read again but I can't store my data Whenever I press the "add" button, it will print out

    No object in the filejava.io.EOFException
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

    What is that error mean ? What must I do ?

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    You're trying to store a String as an Integer. If we could see the code, we could probably be of more help.

  3. #3
    MarMer is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Toll View Post
    You're trying to store a String as an Integer. If we could see the code, we could probably be of more help.
    Yeah,.. Thank you...
    Finally I can found my mistake
    very happy ...

    thank you very much

  4. #4
    MarMer is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Hi...
    I want to ask again...
    To solve my problem that I said in previous time, I use these codes :
    Java Code:
    int i;
     Object cb01 = cb1.getSelectedItem();
                       cb01 = (Object)i;
    And finally I can store the data into files
    but when I want to display it, the output will always be 0
    What is my mistake ?
    Here is my code:
    (for button 'add')

    Java Code:
    public void actionPerformed(ActionEvent e)
        {
           LithosPrisonerMethods LithosPrisoner1 = new LithosPrisonerMethods();
    
               command = group1.getSelection().getActionCommand();
                if(command == a){
                    b = "Male";
                }
                 else if(command == c)
                {
                 b ="Female";
                }
           
               command2 = group2.getSelection().getActionCommand();
               if(command2 == f)
                {
                    d = "Low";
                }
    
                else if(command2 == g)
                {
                    d = "High";
                }
    
    
           
            if(e.getSource() == btn1)
            {
              
                if (tf1.getText().isEmpty() || tf2.getText().isEmpty() || tff1.getValue() == null)
                {
                    JOptionPane.showMessageDialog(this, "Please enter the data needed !");
                }
    
                else{
    
                       int n = JOptionPane.showConfirmDialog(this, "Name: "+tf1.getText()+" "+tf2.getText()
                        +"\nID  :"+tf3.getText()+tff1.getText()+"\nDate of birth: "
                        +cb1.getSelectedItem()+" "+cb2.getSelectedItem()+" "+spin1.getValue()+
                        "\nGender: "+b+"\nDate in:"+
                        cb3.getSelectedItem()+" "+cb4.getSelectedItem()+" "+spin2.getValue()
                        +"\nDate out: "+cb5.getSelectedItem()+" "+cb6.getSelectedItem()+" "
                        +spin3.getValue()+"\nSecurity Level: "+d+"\n"
                        ,"Confirmation", JOptionPane.OK_CANCEL_OPTION);
    
                          if ( n == JOptionPane.OK_OPTION)
                          {   
    
    
                       Object cb01 = cb1.getSelectedItem();
                       cb01 = (Object)i;
                       Object cb03 = cb3.getSelectedItem();
                       cb03 = (Object)i;
                       Object cb05 = cb5.getSelectedItem();
                       cb05 = (Object)i;
    
    
    
                        Object[] op = new Object[15];
                        op[0] = tf1.getText();
                        op[1] = tf2.getText();
                        op[2] = tf3.getText()+tff1.getValue();
                        op[3] = cb01;
                        op[4] = cb2.getSelectedItem();
                        op[5] = spin1.getValue();
                        op[6] = b;
                        op[7] = cb03;
                        op[8] = cb4.getSelectedItem();
                        op[9] = spin2.getValue();
                        op[10] = cb05;
                        op[11] = cb6.getSelectedItem();
                        op[12] = spin3.getValue();
                        op[13] = d;
                        op[14] = ta1.getText();
    
    
                              if(LithosPrisoner1.addPrisoner(op)){
                                     JOptionPane.showMessageDialog(this, "Added Successfully");
                                     tf1.setText(null);
                                     tf2.setText(null);
                                     tff1.setText(null);
                                     cb1.setSelectedIndex(0);
                                     cb2.setSelectedIndex(0);
                                     spin1.setValue(currentYear);
                                     cb3.setSelectedIndex(0);
                                     cb4.setSelectedIndex(0);
                                     spin2.setValue(currentYear);
                                     cb5.setSelectedIndex(0);
                                     cb6.setSelectedIndex(0);
                                     spin3.setValue(currentYear);
                                     ta1.setText(null);
    
    
                              }
    
                              else 
                              {
                                     JOptionPane.showMessageDialog(null, "Please change the ID number, it is already exist !");
                              }
                    }
                }
            }
    tf = JTextField, cb = Combo box, rb = radioButton, spin = JSpinner


    Java Code:
     private ArrayList <LithosPrisoner> thePrisoner = new ArrayList<LithosPrisoner>();
    
    
        public boolean addPrisoner(Object... op)
        {
            PrisonerFiles.read_object(thePrisoner);
    
            String firstName = (String)op[0];
            String surName = (String)op[1];
            String prisonerId = (String)op[2];
            int dobDate = (Integer) op[3];
            String dobMonth = (String)op[4];
            int dobYear = (Integer)op[5];
            String gender = (String)op[6];
            int dinDate =(Integer)op[7];
            String dinMonth = (String)op[8];
            int dinYear = (Integer) op[9];
            int doutDate =(Integer)op[10];
            String doutMonth = (String)op[11];
            int doutYear = (Integer) op[12];
            String secLvl = (String)op[13];
            String reason = (String)op[14];
    
            LithosPrisoner p1 = new LithosPrisoner (firstName,surName,
                    prisonerId,dobDate,dobMonth,dobYear,gender,dinDate,dinMonth,
                    dinYear,doutDate,doutMonth,doutYear,secLvl,reason);
    
            for(int i = 0; i < thePrisoner.size();i++)
            {
                if( thePrisoner.get(i).getPrisonerId().equals((String)op[2]))
                {
                return false;
                }
            }
    
            thePrisoner.add(p1);
            PrisonerFiles.write_object(thePrisoner);
               return true;
    
        }
    Thank you ... :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I see some unrelated problems in your code:

    • Non-intuitive variable names: what are a, b, c, ...? what are cb1, cb2, cb3,... (we know they're combo boxes, but what do they hold)? Would it be more appropriate to use common-sense variable names that make your code self-documenting?
    • It looks like you're trying to compare Strings with the == operator rather than the equals(...) or equalsIgnoreCase(...) method. Using == tests if one String object is the same as another when you don't want to know this. Instead you really want to know if one String holds the same String text as another, and the equals/equalsIgnoreCase will answer this for you.
    • Using one ActionListener for all your action listener needs: I wonder if your whole GUI implements ActionListener. Whether it is or not, you should be using separate action listeners for distinctly separate actions rather than trying to shoe-horn it all into one very difficult to debug "switchboard" listener. Anonymous inner classes work well to help solve this.
    • You get selected items from your combo boxes only to discard them. For instance look what you do with your cb01 variable -- you fill it with the selected item from the cb1 combo box, only to replace it with i (whatever the heck this is?) on the next line. Same with other selections. Why this wasted code?
    • Too much dealing with Object and (?) casting to Object. Better to deal with more specific classes.


    I haven't gone through all of your code as I don't have the time right now, but you may wish to simplify your problem in order to try to isolate it and if not successful at debugging it, at least be able to post compilable runnable code that we can run, test, modify and help correct, an SSCCE.

Similar Threads

  1. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 09:31 AM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  5. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •