Results 1 to 10 of 10
  1. #1
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default How to get rid of the unchecked or unsafe operations warning?

    Hi All,

    My java code have unchecked or unsafe operations waring. I tried to use

    @SuppressWarnings("unchecked") before the code that caused the waring but it caused 4 errors.

    Why does the line

    customer[0].push(1)

    cause the warning to appear?

    Is there a way to get rid of the waring?

    Here is the code in two java files.

    Thank you very much.

    Java Code:
    public class Test
    {
        public Customer[] customer = new Customer[2];
        public static void main(String[] args)
        {
    
               Test t = new Test();
               t.line();
        }
    
        public void line()
    	{
           //@SuppressWarnings("unchecked")
    
            customer[0] = new Customer();
            customer[0].push(1);
    
        }
    }
    
    class StackEmptyException extends Exception
    {
    	public StackEmptyException()
    	{
    
    	}
    }
    
    class Customer<E> extends Queue<E>
    {
    	private int time;
    
        public void push(E e)
    	{
    	        super.addToEnd(e);
        }
    }
    Java Code:
    import java.util.*;
    
    public class Queue<E>
    {
    	private static class Node<E>
    	{
    		public E data;
    		public Node<E> next;
    
    		public Node(E data, Node<E> next)
    		{
    			this.data = data;
    			this.next = next;
    		}
    		public E get()
    		{
    		    return this.data;
    		}
    	}
    
    	private Node<E> head;
    
    	private int count;
    
    	public Queue()
    	{
    		head = null;
    	}
    
    	 public void addToEnd(E e)
    	{
    		E d;
    		Node<E> c=head;
    	    if (c == null)
    	    {
    	         head = new Node<E>(e,head); //add first node
    	    }
    	    else
    	    {
    	         //head = c;
    	         while ( c.next!=null)
    			 {
    				  c=c.next;
    		     }
    	         c.next = new Node<E>(e,null);  //add new node at the end
    	    }
             count++;
            // return head;
    	}
    }

  2. #2
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    You get warnings because you haven't typed your Customer array or your new instance of Customer. If Customer[] is always going to be the same type, then type it in the declaration (Customer<Integer>[] for example). If it's not always the same type, then you'll have to declare it as Customer<?>[].

    When you create a new instance of Customer, type it (new Customer<Integer>() for example.

    If you type Customer[] with Integer, then your push warning should go away. If you type it with the ? wildcard, then you'll always have a warning.

  3. #3
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default

    Thank for your quick reply. I try this

    public Customer<Customer>[] customer = new Customer[2];

    I got an error. The warning is still there.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You want to parameterize it with the type of info you are gonna store.

  5. #5
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default

    I tried to use <String> to replace <Customer>. The error is gone but the warning is still there. I removed

    private int time;

    since I do not need it.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Possibly you have other locations in your code where you do not use Generics. Recompile with the -Xlint:unchecked option to get more details.

  7. #7
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default

    I tried it with -Xlint:unchecked. It says "unchecked call to push(E) as a member of the raw type Customer. Only customer[0].push(1) causes the warning. If I delete it, no warnong will appear.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You want to parameterize customer and then wherever you see an e it should pass the parameterized type. If you parameterize it with type String, then the argument to push should be a string.

  9. #9
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    Morning! There were 2 things I mentioned that you need to parametrize. The declaration and the constructor. Did you do both?

    Also, as sunde887 pointed out, if you parametrize with a String, yet you use an Integer later, you will get a compile error because you're using a type that is not what you specified.

  10. #10
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default

    I do not know how to do it. I googled it and did not find the details. As I said, when I inserted <String> or <Integer> , it gave an error and the warning. The data field in the Node will store an int. All code are there in the top post. Can you test it to see if parametrize them will work?

Similar Threads

  1. Unchecked or Unsafe Operations Error Message
    By littleone in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 01:50 PM
  2. unchecked or unsafe operations-Recompile with -Xlint
    By pronetin in forum Advanced Java
    Replies: 15
    Last Post: 05-31-2010, 05:41 PM
  3. Unchecked or unsafe operations warning
    By sky in forum New To Java
    Replies: 3
    Last Post: 12-06-2009, 03:41 AM
  4. Replies: 22
    Last Post: 05-08-2008, 12:00 PM
  5. Uses unchecked or unsafe operations message
    By Robbinz in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 10: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
  •