File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes using collections in Java 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using collections in Java 5" Watch "using collections in Java 5" New topic
Author

using collections in Java 5

Pierre Bungle
Greenhorn

Joined: May 05, 2009
Posts: 9
Hello everyone!

I'm following a bunch of online tutorials I found to get used to using Swing in Java, and the one I'm using now is great, but pre-dates java 5. As a result, the code won't compile, and I'm being warned about "unchecked or unsafe operations". After searching online, it turns out that I should be adding <String> to one (or more?) of my variables, but I'm too much of a greenhorn to figure out where. I've played around a bit, but can't solve it. As far as I can understand, <String> should be added to one or more of the variable in lines 16, 17, and/or 28. When I do, however, I get an error in line 87, and the compiler complains that it wants a String, but is being fed a StringBuffer.

Any help/hints would be much appreciated!

Here's the code:



Thank's in advance, y'all!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Pierre Bungle wrote:I'm following a bunch of online tutorials I found to get used to using Swing in Java, and the one I'm using now is great, but pre-dates java 5. As a result, the code won't compile, and I'm being warned about "unchecked or unsafe operations". After searching online, it turns out that I should be adding <String> to one (or more?) of my variables, but I'm too much of a greenhorn to figure out where. I've played around a bit, but can't solve it. As far as I can understand, <String> should be added to one or more of the variable in lines 16, 17, and/or 28. When I do, however, I get an error in line 87, and the compiler complains that it wants a String, but is being fed a StringBuffer.

The problem is at line 87:
model.addElement(buffer);

You have two choices, and which one you want is really up to you:
1. Make your 'model' instance a DefaultListModel<CharSequence>. That will allow you to add either a String or a StringBuffer.
(I have to admit I can't see where it should be requiring a String, but maybe it's buried somewhere else in your code. As it stands, I would expect it to throw a NullPointerException, because you don't seem to initialize the list at all).

2. Change line 87 to:
model.addElement(buffer.toString());

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Pierre Bungle
Greenhorn

Joined: May 05, 2009
Posts: 9
I'm not sure I understand the first solution, but I will delve deeper into it later.

Second one worked a treat, thank you very much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using collections in Java 5