Results 1 to 19 of 19
  1. #1
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default Compile error - unchecked

    I created this class a couple months ago and it compiled fine, i made no changes, other that putting in a println to test the writeService method (just got to that part in my main app), now I am getting an unchecked error and I don't understand what it is complaining about.

    Error message:
    new-host-3:~/Desktop] mike% javac -Xlint:unchecked VIN.java
    VIN.java:143: warning: [unchecked] unchecked conversion
    found : java.util.ArrayList
    required: java.util.ArrayList<VIN>
    VINrecords = (ArrayList) oisVehicleFile.readObject();
    ^
    1 warning
    [new-host-3:~/Desktop] mike%



    Code (stripped down to the essentials):
    Java Code:
    import java.io.*;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.lang.Number.*;
    import java.util.*;
    import java.util.prefs.*;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    import javax.swing.filechooser.FileFilter;
    
    
    /**
     * Maintain a vehicle history
     * @author	Mike Lipay
     * @date	12/17/2009
     * @version	1.0
    */
    public class VIN implements Serializable
    {
    	private static final long serialyearUID = 5231720162899345259L;
    	static ObjectOutput      oosVehicleFile;
    	static FileInputStream   fisVehicleFile;
    	static ObjectInputStream oisVehicleFile;
    
    	final String fileSep  = System.getProperty("file.separator");
    	final String fieldSep = "\t";
    
    	static String vhDir   = "vhDir";
    	static String vehicleFile;
    
    	ArrayList <VIN> VINrecords = new ArrayList <VIN> ();
    	String type;
    	String record;
    
    
    /**
     * Blank constructor
    */
    	public VIN ()
    	{
    	}
    
    
    /**
     * Main constructor
     * @param	aType	   Record type (Title, License, etc.)
     * @param	aRecord  One entry from the ArrayList
    */
    	public VIN ( String aType, String aRecord )
    	{
    		type   = aType;
    		record = aRecord;
    	}
    
    
    	public String getType()   { return type; }
    	public String getRecord() { return record; }
    
    
    /**
     * Handle the Service Record
    */
    // Read the Service record
    	public String readService (String aVIN)
    	{
    		if (VINrecords.size() == 0) loadVIN(aVIN);
    
    		for ( VIN VINentry : VINrecords )
    			if (VINentry.getType().equals("SVC"))
    				return (VINrecords.indexOf(VINentry) + fieldSep + VINentry.getRecord());
    
    		return (null);
    	}
    
    
    // Read the Next Service record
    	public String readNextService (int aIdx, String aVIN)
    	{
    		int idx = 0;
    		
    		if (VINrecords.size() == 0) loadVIN(aVIN);
    
    		for ( VIN VINentry : VINrecords )
    		{
    			if (aIdx < idx)
    			{
    			if (VINentry.getType().equals("SVC"))
    				return (VINrecords.indexOf(VINentry) + fieldSep + VINentry.getRecord());
    			}
    			else idx++;
    		}
    
    		return (null);
    	}
    
    
    // Write the Service record
    	public int writeService (String aVIN, String aRecord)
    	{
    		String [] temp = aRecord.split(fieldSep, 2);
    		int idx = Integer.parseInt(temp[0]);
    		VIN tempVIN = new VIN("SVC",temp[1]);
    		System.out.println(temp[1]);
    
    		if (idx > -1)
    			VINrecords.set (idx, tempVIN);
    		else
    		{
    			VINrecords.add (tempVIN);
    		}
    
    		saveVIN(aVIN);
    		return (VINrecords.indexOf(tempVIN));
    	}
    
    
    
    /**
     * The section below handles the actual file I/O
    */
    
    // Load the VIN file
    	public void loadVIN (String aVIN)
    	{
    		Preferences root = Preferences.userRoot();
    		Preferences node = root.node(fileSep + "com"
    							+ fileSep + "lipay"
    							+ fileSep + "vhReg");
    		vhDir = node.get("vhDir", "na");
    
    		if (vhDir.equals("na"))	System.exit(1);
    
    		vehicleFile = vhDir + fileSep + aVIN + ".vin";
    		File testFile = new File (vehicleFile);
    		if (!testFile.exists())	return;
    
    		try
    		{
    			fisVehicleFile = new FileInputStream(vehicleFile);
    			oisVehicleFile = new ObjectInputStream(fisVehicleFile);
    			VINrecords = (ArrayList) oisVehicleFile.readObject();
    			oisVehicleFile.close();
    			fisVehicleFile.close();
    		}
    		catch (EOFException e1)
    		{
    			try
    			{
    				oosVehicleFile = new ObjectOutputStream(new FileOutputStream(vehicleFile));
    				oosVehicleFile.writeObject(VINrecords);
    				oosVehicleFile.close();
    			}
    			catch (Exception e2)	{ e2.printStackTrace(); }
    		}
    		catch (Exception e1)	{ e1.printStackTrace(); }
    	}
    
    
    
    // Save the VIN file
    	public void saveVIN (String aVIN)
    	{
    		try {
    			oosVehicleFile = new ObjectOutputStream(new FileOutputStream(vehicleFile));
    			oosVehicleFile.writeObject(VINrecords);
    			oosVehicleFile.close();
    		}
    		catch (Exception e) { e.printStackTrace(); }
    	}
    }
    Last edited by Eranga; 06-20-2010 at 04:54 PM. Reason: code tags added

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    That's not an error, but rather it is a warning. It won't stop your code from compiling or running. It's telling you that you have code that is not using generics but that should, that's all. Also, please use code tags when posting code so that we'll have an easier time reading and understanding your code. The forum FAQ's can tell you how to do this.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you run the code, since there is no any errors shows? Try out those not harm at all, but you can learn something. Those tryouts are more valuable, before asking any question. ;)

  4. #4
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Sorry about the code tags, I didn't recognize the # symbol as code.

    Yes it does run, and I understand it's a warning only. What I didn't get is why it just started throwing the error, it didn't when I last compiled it.

    What are generics?

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You are likely compiling with a different version of Java as generics have been part of Java since Java 5. Google for Java generics tutorials for decent description and instruction on what generics are.

  6. #6
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Ok, thanks, that did clear things up. Unfortunately, when I apply the changes I get an error on the readObject(). I will have to investigate this later when I have more time.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pahiker View Post
    Sorry about the code tags, I didn't recognize the # symbol as code.
    I've added code tags in your code segment. If you want to know more about how to add those code tags, check on my forum signature.

    Quote Originally Posted by pahiker View Post
    Yes it does run, and I understand it's a warning only. What I didn't get is why it just started throwing the error, it didn't when I last compiled it.
    Fine. Hope you've look at that line of code where you get the warning message in your code. Warnings never treat as errors or exceptions in your code, because it's just a matter of the JDK version compatibilities.

    Quote Originally Posted by pahiker View Post
    What are generics?
    In definition, Generics are allowed applications to create classes and objects that can operate on any defined types. If you are not clear with that read this page first of all, and try out the examples.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pahiker View Post
    Ok, thanks, that did clear things up. Unfortunately, when I apply the changes I get an error on the readObject(). I will have to investigate this later when I have more time.
    What changes you've done? Actually if you are still not clear with generics in Java best things is read bit more about it, and then try to apply it in your code. :) I've send a nice link explaining in briefly, you can read it within an hour and apply it to your code. Give a try and see. Good luck!

  9. #9
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    What changes you've done? Actually if you are still not clear with generics in Java best things is read bit more about it, and then try to apply it in your code. :) I've send a nice link explaining in briefly, you can read it within an hour and apply it to your code. Give a try and see. Good luck!
    I did read it, and applied the changes, that is what I meant above. After applying the changes suggested by the article I am now getting an incompatibility error on the readObject(). I understand (pretty sure) why I am getting that error, but that is a compile-stopper. At this point I need to finish up the changes that I am making (with the warning) then go back and try to resolve the readObject() issue. The warning doesn't stop the app from running, so I can live with it until I finish the changes in the main app.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    So doing this:
    Java Code:
    VINrecords = (ArrayList<VIN>) oisVehicleFile.readObject();
    gives you a compilation error?

  11. #11
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Don't know, didn't try that.

    What I gleaned from the article is that the move is to remove the (ArrayList) from the code, that it is to go on the declaration and that the code should simple state:

    Java Code:
    ArrayList <VIN> VINrecords = new ArrayList <VIN> ();
    
    VINrecords = oisVehicleFile.readObject();
    Did I misunderstand the article?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    The API for ObjectInputStream shows that readObject() returns an Object. So you can't simply assign that to an ArrayList<VIN>, hence the compilation error.

    So what you do is tell the compiler that you know this is actually an ArrayList<VIN>, which is what the line I wrote above does.

    Your first go at this simply told it it was an ArrayList, so the compiler flagged a warning, because though it's allowed, you should really say what Class an ArrayList stores.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pahiker View Post
    I did read it, and applied the changes, that is what I meant above. After applying the changes suggested by the article I am now getting an incompatibility error on the readObject(). I understand (pretty sure) why I am getting that error, but that is a compile-stopper. At this point I need to finish up the changes that I am making (with the warning) then go back and try to resolve the readObject() issue. The warning doesn't stop the app from running, so I can live with it until I finish the changes in the main app.
    That's why I told you not to apply it until you don't have a better understanding.

    Actually if you are still not clear with generics in Java best things is read bit more about it, and then try to apply it in your code
    Can you post the complete error message?

  14. #14
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    That's why I told you not to apply it until you don't have a better understanding.
    That's why I said that I am shelving this issue until I have time to investigate it more fully. Right now the warning is not a show-stopper, the compile error is. I can hold off until I complete work on the segment that I am currently working on. In a few days, week at the outside, I will have time to revisit this.

    Thanks for your help and suggestions.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    I thought my explanation summed it all up nicely...

  16. #16
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Huh, I never got your reply. Clicking on Eranga's link to me straight to her reply, so I never read yours. Sorry about that.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pahiker View Post
    That's why I said that I am shelving this issue until I have time to investigate it more fully. Right now the warning is not a show-stopper, the compile error is. I can hold off until I complete work on the segment that I am currently working on. In a few days, week at the outside, I will have time to revisit this.

    Thanks for your help and suggestions.
    That's fine. Good luck!

  18. #18
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Ok, I got the time to read (a lot) on generics, and to try and solve this problem.

    Tolls, no, your suggestion didn't work. In fact, nothing I tried worked. I did a search on unchecked and readObject, and I found an article by someone named Tom Hawtin who says that there is no way to get this to work with readObject, his solution was to incorporate @SuppressWarnings("unchecked"), but did not go any further than that.

    Where do you add the code? I've tried a couple of different places and cannot find anywhere that it compiles without still throwing the unchecked error. Sun's doc does not specifically say where to put it.


    <<update>>
    I found out where to put it (yeah, I know, not there), but I didn't want to delete this in case someone was following. Here is the code:

    Java Code:
    
    // Load the VIN file
    	@SuppressWarnings("unchecked")
    	public void loadVIN (String aVIN)
    	{
    Last edited by pahiker; 06-24-2010 at 12:38 AM. Reason: found problem

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    No, you will always get the unchecked cast warning because you are doing an unchecked cast.
    Nothing you can do to avoid that.

    My suggestion did work, in that the unchecked warning is at least warning about something you can't avoid, which is the cast from Object to ArrayList<VIN>. And not your original one of ArrayList to ArrayList<VIN>.

Similar Threads

  1. Compile error in Driver class, help plz
    By Battlefeldt in forum New To Java
    Replies: 3
    Last Post: 11-25-2009, 11:30 PM
  2. compile error
    By angryredantz in forum New To Java
    Replies: 1
    Last Post: 01-23-2009, 10:44 PM
  3. Compile Error - Please Help!!
    By AJ2009 in forum New To Java
    Replies: 10
    Last Post: 01-04-2009, 03:59 PM
  4. Java 1.5 compile time error
    By ank_k in forum New To Java
    Replies: 4
    Last Post: 11-13-2008, 11:12 AM
  5. compile error
    By dirtycash in forum New To Java
    Replies: 6
    Last Post: 12-12-2007, 06:00 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
  •