• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Troublesome logic error

 
Eric Larsen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following app is designed to play music using javax.sound.midi. It has three menu items, new, save, and load. New and save work as expected, but load does not. The load menu item has an ActionListener, LoadMenuListener. LoadMenuListener's actionPerformed() method opens the JFileChooser and passes the selected file to loadFile(File file). In loadFile(File file), a BufferedReader is chained to a FileReader(file). Then, the BufferedReader reads the only line in the file. This line contains 0's and 1's separated by commas. String.split(",") is called on the line and the result is stored as an array of Strings. Then, the program steps through each element and does an if/else/if:

based on the results, it sets the element of checkBoxList in question (a JCheckBox) to either true or false.

That is what SHOULD happen. What REALLY happens however, is that it does not change the state of any JCheckBox in checkBoxList. It give's no run-time errors.

Thanks in advance for the help!

Here is the complete code:


Attached is a picture of the gui.
guiPic.JPG
[Thumbnail for guiPic.JPG]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't compare the contents of Strings using the "==" operator; this operator returns true if and only if two Strings represent the same physical object in memory, and a String read from a file will never be the same physical object as a literal embedded in a program (like your "0" and "1"). Instead, you want to compare the contents of the Strings; for this you must use the equals() method:



Beware of NullPointerExceptions -- you'll get one with this code if result[cellNum] is ever null.
 
Eric Larsen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I had a feeling it was something simple like that, but I had no idea where to look (as far as resources go) to find out what was wrong. The API only includes information specific to classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic