• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

using collections in Java 5

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Pierre Bungle
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic