Results 1 to 9 of 9
  1. #1
    KillTheCube is offline Member
    Join Date
    Dec 2011
    Location
    Wisconsin
    Posts
    6
    Rep Power
    0

    Default How to fix the unchecked or unsafe opeartions error?

    I'v been learning java on my own for a while now and I'm using Sam's Teach Yourself Java 6 in 21 days. In a few of the coding examples they give I get and unchecked or unsafe operations error and I'm not sure how to fix it. I've looked on google and I've seen examples using generics, but I can't figure out how to implement.

    Code causing the problem:
    Java Code:
    for (int i = 0; i < formats.length; i++)
        formatBox.addItem(formats[i]);
    I've looked in my book and it says that the addItem method can take a method as an object, but when I change the input into the addItem method and use a variable without the for loop then I get an incorrect type error. It says it expects an int.
    <<<< Link removed. Please post the code here>>>>>>>
    The link is to the full code, if you need more information please let me know and thanks for all the help.
    Last edited by Norm; 12-13-2011 at 06:32 PM. Reason: Removed link

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: How to fix the unchecked or unsafe opeartions error?

    Put the code in the post and remove the link to get more people to look at this.

  3. #3
    KillTheCube is offline Member
    Join Date
    Dec 2011
    Location
    Wisconsin
    Posts
    6
    Rep Power
    0

    Default Re: How to fix the unchecked or unsafe opeartions error?

    Here's the full the code that I'm dealing with
    Java Code:
    import javax.swing.*;
    
    public class FormatFrame2 extends JFrame {
        String[] formats = { "Atom", "RSS 0.92", "RSS 1.0", "RSS 2.0" };
        JComboBox formatBox = new JComboBox();
        
        public FormatFrame2() {
            super("Choose a Format");
            setSize(220, 150);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel pane = new JPanel();
            JLabel formatLabel = new JLabel("Output formats:");
            pane.add(formatLabel);
            for (int i = 0; i < formats.length; i++)
                formatBox.addItem(formats[i]);
            pane.add(formatBox);
            add(pane);
            setVisible(true);
        }
    
        public static void main(String[] arguments) {
            FormatFrame2 ff = new FormatFrame2();
        }
    }

  4. #4
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: How to fix the unchecked or unsafe opeartions error?

    Quote Originally Posted by KillTheCube View Post
    Here's the full the code that I'm dealing with
    Java Code:
    import javax.swing.*;
    
    public class FormatFrame2 extends JFrame {
        String[] formats = { "Atom", "RSS 0.92", "RSS 1.0", "RSS 2.0" };
        JComboBox formatBox = new JComboBox();
        
        public FormatFrame2() {
            super("Choose a Format");
            setSize(220, 150);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel pane = new JPanel();
            JLabel formatLabel = new JLabel("Output formats:");
            pane.add(formatLabel);
            for (int i = 0; i < formats.length; i++)
                formatBox.addItem(formats[i]);
            pane.add(formatBox);
            add(pane);
            setVisible(true);
        }
    
        public static void main(String[] arguments) {
            FormatFrame2 ff = new FormatFrame2();
        }
    }
    Post the error you are getting with this code.

    If you have changed the code, repost it WITH the error you are getting for it.

  5. #5
    KillTheCube is offline Member
    Join Date
    Dec 2011
    Location
    Wisconsin
    Posts
    6
    Rep Power
    0

    Default Re: How to fix the unchecked or unsafe opeartions error?

    Note: FomratFrame2.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    this is the error code that I get with the original code.

    Java Code:
    import javax.swing.*;
    
    public class FormatFrame2 extends JFrame {
        String[] formats = { "Atom", "RSS 0.92", "RSS 1.0", "RSS 2.0" };
        JComboBox formatBox = new JComboBox();
        
        public FormatFrame2() {
            super("Choose a Format");
            setSize(220, 150);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel pane = new JPanel();
            JLabel formatLabel = new JLabel("Output formats:");
            pane.add(formatLabel);
    		int con;
    		formatBox.addItem(con = Integer.parseInt(formats));
     //       for (int i = 0; i < formats.length; i++)
     //           formatBox.addItem(formats[i]);
            pane.add(formatBox);
            add(pane);
            setVisible(true);
        }
    
        public static void main(String[] arguments) {
            FormatFrame2 ff = new FormatFrame2();
        }
    }
    With this new code I commented out the for loop that would iterate the string into the drop box. After that I created a variable named con and thought I might be able to use a conversion to trick into thinking it was receiving the required input.
    New Error:
    not suitable method found for parsInt(String[]), then it gives 15th line of code.
    method Integer.parseInt(String, int) is not applicable(actual and formal agument lists differ in length)
    Note: FomratFrame2.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    I'm pretty new to java so I don't know if I'm making a weird mistake or if this fix I tried to use for the code was even close helping.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: How to fix the unchecked or unsafe opeartions error?

    Recompile with -Xlint:unchecked for details.
    That's a warning message from the compiler. It's telling you to add the -Xlint option to the compiler's options to get the full text of the warnings.
    Here's a sample commandline for compiling a java program:
    F:\Java\jdk1.6.0_29\bin\javac.exe -Xlint -g -deprecation -classpath D:\JavaDevelopment\;.;..\. SavingBinaryTree.java

    not suitable method found for parsInt(String[]),
    You are not using the method correctly. Read the API doc for the Integer class to get the correct syntax for the parseInt method.
    http://download.oracle.com/docs/cd/E...se/6/docs/api/

  7. #7
    KillTheCube is offline Member
    Join Date
    Dec 2011
    Location
    Wisconsin
    Posts
    6
    Rep Power
    0

    Default Re: How to fix the unchecked or unsafe opeartions error?

    Okay I read that doc and realize why that conversion wouldn't work, I also ran the -Xlint:unchecked and I have the error. I'm gonna try and do some more research on my own. If I can find a solution then I'll make sure that I post it here, if not I'll be back with questions. Thanks for the help so far.

  8. #8
    KillTheCube is offline Member
    Join Date
    Dec 2011
    Location
    Wisconsin
    Posts
    6
    Rep Power
    0

    Default Re: How to fix the unchecked or unsafe opeartions error?

    Okay so I messed with my code and here's the new code:

    Java Code:
    import javax.swing.*;
    
    public class FormatFrame2 extends JFrame {
        String[] formats = { "Atom", "RSS 0.92", "RSS 1.0", "RSS 2.0" };
        JComboBox formatBox = new JComboBox();
        
        public FormatFrame2() {
            super("Choose a Format");
            setSize(220, 150);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel pane = new JPanel();
            JLabel formatLabel = new JLabel("Output formats:");
            pane.add(formatLabel);
            for (int i = 0; i < formats.length; i++)
    //            formatBox.addItem(formats[i]);
            pane.add(this);
            add(pane);
            setVisible(true);
        }
    
        public static void main(String[] arguments) {
            FormatFrame2 ff = new FormatFrame2();
        }
    }
    It compiles fine, but then when I try to run I get a new error:

    Exception in thread "main" java.lang.IllegalArgumentException: adding a windwo to a container
    at java.awt.Container.checkNotAWindow(Container.java: 483)
    at java.awt.Container.addImpl(Container.java:1084)
    at java.awt.Copntainer.add(Container.java:410)
    at FormatFrame2.(init)(FormatFrame2.java:16)
    at FormatFram2.main(FormatFrame2.java:22)

    It's 4 in the morning so I haven't done any kind of research on this error at all, just thought that I would give an update. If I find something before a post then I'll post what happens here.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: How to fix the unchecked or unsafe opeartions error?

    Exception in thread "main" java.lang.IllegalArgumentException: adding a windwo to a container
    The message is self explanatory. Look at what the type of component you are trying to add to a container on line 16.

Similar Threads

  1. Replies: 9
    Last Post: 05-11-2011, 05:31 PM
  2. Unchecked or Unsafe Operations Error Message
    By littleone in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 02:50 PM
  3. Unchecked or unsafe operations warning
    By sky in forum New To Java
    Replies: 3
    Last Post: 12-06-2009, 04:41 AM
  4. Replies: 22
    Last Post: 05-08-2008, 01:00 PM
  5. Uses unchecked or unsafe operations message
    By Robbinz in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 11:56 PM

Posting Permissions

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