Results 1 to 19 of 19
Thread: Compile error - unchecked
- 06-20-2010, 04:41 PM #1
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
- 06-20-2010, 04:49 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 06-20-2010, 04:56 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. ;)
- 06-20-2010, 05:00 PM #4
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?
- 06-20-2010, 05:02 PM #5
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 06-20-2010, 05:22 PM #6
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.
- 06-21-2010, 07:16 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
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.
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.
- 06-21-2010, 07:20 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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!
- 06-21-2010, 10:23 AM #9
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.
- 06-21-2010, 11:25 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
So doing this:
gives you a compilation error?Java Code:VINrecords = (ArrayList<VIN>) oisVehicleFile.readObject();
- 06-21-2010, 05:12 PM #11
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:
Did I misunderstand the article?Java Code:ArrayList <VIN> VINrecords = new ArrayList <VIN> (); VINrecords = oisVehicleFile.readObject();
- 06-21-2010, 05:30 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
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.
- 06-22-2010, 12:04 PM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-22-2010, 05:09 PM #14
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.
- 06-22-2010, 05:18 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
I thought my explanation summed it all up nicely...
- 06-22-2010, 08:35 PM #16
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.
- 06-23-2010, 04:12 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-24-2010, 12:34 AM #18
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
- 06-24-2010, 09:27 AM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
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
-
Compile error in Driver class, help plz
By Battlefeldt in forum New To JavaReplies: 3Last Post: 11-25-2009, 11:30 PM -
compile error
By angryredantz in forum New To JavaReplies: 1Last Post: 01-23-2009, 10:44 PM -
Compile Error - Please Help!!
By AJ2009 in forum New To JavaReplies: 10Last Post: 01-04-2009, 03:59 PM -
Java 1.5 compile time error
By ank_k in forum New To JavaReplies: 4Last Post: 11-13-2008, 11:12 AM -
compile error
By dirtycash in forum New To JavaReplies: 6Last Post: 12-12-2007, 06:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks