• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap iterating problems  RSS feed

 
mandlar suurla
Ranch Hand
Posts: 67
Debian Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought to use HashMap to make my life easier but it wasn't true, didnt want to write like 10 fields .setText(some value). I don't know how to iterate map which has key some kinda object.


[ July 14, 2008: Message edited by: mandlar suurla ]
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't your keyset going to be a set of JTextComponent? you should be casting the keys to that type instead of String.
 
mandlar suurla
Ranch Hand
Posts: 67
Debian Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeha i forgot to delete my testing with String type here is right code. Still get null pointer exception.

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What variable is null that is causing the exception?
Copy and paste the full text of your error message here. If there is a reference to a line number in your program, copy and comment the lines surrounding the line noted with the error.
Use the javac -g option to include line numbers in the class file.
[ July 14, 2008: Message edited by: Norm Radder ]
 
mandlar suurla
Ranch Hand
Posts: 67
Debian Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok caught problem. That method was called out too early so the components got null declaration.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're going to run into a ClassCastException anytime soon, because the value for key portText is most likely an Integer, not a String.

On a side note, this way of iterating is not the most optimal. You iterate through all the keys, then get their values. The following is a bit faster, since it only iterates (with the same speed) and omits the get operation (note: I use the new for loop with generics; using an iterator works the same way):
 
mandlar suurla
Ranch Hand
Posts: 67
Debian Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your post, it cleared some things like looping map and yes port is Integer but i thought to make all values to String so the conversion would be easier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!