Input dialog problem

Hi everybody,

I'm working on a program that needs to input files for a fileReader, and whenever I try to do the input (click OK or even cancel on the dialog box) the application terminates. I can't figure out what I'm doing wrong, can anyone help me out?

Thanks!

Jezzica85

do{

// Give option for first file input

fileMined = JOptionPane.showInputDialog( null,

"Enter READY When Input Complete",

"Input File or Analyze",

JOptionPane.PLAIN_MESSAGE );

// If no input given, close program

if( ( fileMined.equals("" ) ) && ( numberOfFiles == 0 ) ){

System.exit( -1 );

// If no input given but files previously read, prepare main window

}elseif( ( fileMined.equals("" ) ) && ( numberOfFiles != 0 ) ){

fileMined ="READY";

// If input is given, read in the new file

}elseif( !fileMined.equals("READY" ) ){

readFile();

JOptionPane.showMessageDialog( null,"File Has Been Read." );

numberOfFiles++;

}

}while( !fileMined.equals("READY" ) );

fileMined is declared as a String, and readFile() does the actual file work.

[1949 byte] By [jezzica85a] at [2007-10-3 4:34:35]
# 1

> whenever I try to do the input (click OK or even cancel on the dialog box) the application terminates.

Works fine for me when I click OK

Get a NullPointerException when I click Cancel.

I had to create my own test program. So my code may be differenct than yours.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi, thanks for the answer, and especially for pointing out that NullPointerException (no pun intended).

I'm starting to think the problem is something with my JVM--I got this message when I tried to run my applet and didn't see it before--

Cannot convert string "-monotype-arial-regular-r-normal--*-140-*-*-p-*-iso8859-1" to type FontStruct

Does that mean that for some reason my JVM can't recognize my text at all? If that was the case it would satisfy my first if condition and explain why the application always closes. I'm not sure if that makes sense, but it's an idea.

Message was edited by:

jezzica85

jezzica85a at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 3
That looks like some sort of bug. It could be just exiting because the VM is bombing out.Maybe it just doesn't like your L&F for some reason? I dunno. That type FontStruct is certainly a VM error.
cotton.ma at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 4
That makes sense, thanks...but um, what's an L&F? I've never heard that before.
jezzica85a at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 5
> That makes sense, thanks...but um, what's an L&F?> I've never heard that before.The look and feel your GUI is using. It doesn't seem to like one of the fonts.Could you post some code that can be compiled and run as is? No modifications.
cotton.ma at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 6
What do you mean, compiled and run as is? The code I posted works, the input box initializes and everything. It goes crazy as soon as I try to press one of the buttons, and closes the application. Do you want me to write more code?
jezzica85a at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 7

> What do you mean, compiled and run as is? The code I

> posted works, the input box initializes and

> everything. It goes crazy as soon as I try to press

> one of the buttons, and closes the application. Do

> you want me to write more code?

You posted a piece of your program. But by itself it won't compile or run. I'd like to see all the code.

cotton.ma at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 8

Silly me! Here's the code, hopefully it formats right. I haven't implemented actionPerformed() yet but I will once I have to use the buttons.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.lang.*;

import java.io.*;

/**

* This class constructs and runs a document analysis utility.

*/

public class WordMiner implements ActionListener {

private ArrayList<Integer> individualChapterLengths;

private ArrayList<Integer> uniqueWordsByChapter;

private ArrayList<JButton> theChapterButtons;

private ArrayList<String> allTheWords;

private int numberOfFiles;

private String fileMined;

private JFrame minerView;

private JPanel mainPanel;

/**

* Default constructor

*/

public WordMiner() {

individualChapterLengths = new ArrayList<Integer>();

uniqueWordsByChapter = new ArrayList<Integer>();

theChapterButtons = new ArrayList<JButton>();

allTheWords = new ArrayList<String>();

numberOfFiles = 0;

// Set up the main frame

minerView = new JFrame( "The Word Miner version 1.0" );

Container pane = minerView.getContentPane();

minerView.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

do {

// Give option for first file input

fileMined = JOptionPane.showInputDialog( null,

"Enter READY When Input Complete",

"Input File or Analyze",

JOptionPane.PLAIN_MESSAGE );

// If no input given, close program

if( ( ( fileMined.equals( "" ) ) && ( numberOfFiles == 0 ) ) ||

( fileMined.equals( null ) ) ) {

System.exit( -1 );

// If no input given but files previously read, prepare main window

} else if( ( fileMined.equals( "" ) ) && ( numberOfFiles != 0 ) ) {

fileMined = "READY";

// If input is given, read in the new file

} else if( !fileMined.equals( "READY" ) ) {

readFile();

JOptionPane.showMessageDialog( null, "File Has Been Read." );

numberOfFiles++;

}

} while( !fileMined.equals( "READY" ) );

JButton frequency = new JButton( "Frequencies" );

frequency.setForeground( Color.BLACK );

frequency.setBackground( Color.RED );

mainPanel.add( frequency );

JButton totalButton = new JButton( "Total Statistics" );

totalButton.setForeground( Color.BLACK );

totalButton.setBackground( Color.RED );

mainPanel.add( totalButton );

pane.add( mainPanel, BorderLayout.CENTER );

minerView.pack();

minerView.setVisible( true );

}

/**

* Reads in text files and sets up access to them

*/

public void readFile() {

try {

// Create button for new file

JButton button = new JButton( fileMined );

button.setForeground( Color.BLACK );

button.setBackground( Color.CYAN );

button.addActionListener( this );

theChapterButtons.add( button );

mainPanel.add( button );

// Read the file and skip the first line

FileReader reader = new FileReader( fileMined );

BufferedReader buffReader = new BufferedReader( reader );

buffReader.readLine();

String line = buffReader.readLine();

// Set up the data lists

Vector chapterAll = new Vector();

HashSet<String> chapterUniques = new HashSet<String>();

// Scan and parse the file

String dels = "\0009\0032\0033\0040\0041\0044\0058\0059\0063" +

"\0133\0145\0146\0147\0148\0151";

boolean numberLine = false;

try {

Integer.parseInt( line );

numberLine = true;

} catch( Exception e ) {

numberLine = false;

}

while( line != null ) {

if( ( line.length() > 2 ) && ( numberLine == false ) ) {

StringTokenizer parser = new StringTokenizer( line, dels );

while( parser.hasMoreTokens() ) {

String temp = parser.nextToken();

String finalWord = temp.toLowerCase();

chapterUniques.add( finalWord );

allTheWords.add( finalWord );

}

} else {

return;

}

}

individualChapterLengths.add( chapterAll.size() );

uniqueWordsByChapter.add( chapterUniques.size() );

} catch( IOException badIO ) {

System.exit( -1 );

}

}

/**

* Handles actions

*

* @param e the event to handle

*/

public void actionPerformed( ActionEvent e ) {

}

/**

* The main method.

*

* @param args the command line arguments (ignored)

*/

public static void main( String args[] ) {

try {

WordMiner miner = new WordMiner();

} catch( Exception e ) {

System.exit( -1 );

}

}

}

// WordMiner.java

jezzica85a at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 9

Yes that was helpful.

Please if there is nothing else you take away from this assignment NEVER EVER have empty catch blocks or catch blocks that just exit like that. It makes it very difficult to debug.

You may have some problem with regards to your font or whatever still but I did find another NullPointerException.

I added stack traces to your various catch blocks and the one that did it was

public static void main( String args[] ) {

try {

WordMiner miner = new WordMiner();

} catch( Exception e ) {

e.printStackTrace(); // NEW

System.exit( -1 );

}

}

Sure enough after the opening dialog it blew up with a Null Pointer Exception on line 97. My line 97 (your mileage may vary) is

mainPanel.add( button );

So I looked about and realized that mainPanel never gets initialized so there you go.

cotton.ma at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 10

Thanks a lot for that! That's a great idea with printStackTrace().

Unfortunately, my program still closes once I hit any button on the input dialog, and it isn't throwing exceptions now. Do you by any chance have any other ideas?

I was thinking, maybe if I told it to use a different font it would work, but I don't know how to do that.

jezzica85a at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 11

You have similar type of empty catch blocks in a few places. Every place you have a System exit you might want to put a System.out.println documenting where in the code you are exiting from.

So far in my tests I have had a FileNotFoundException (there is a catch block with IOException you exit from)

and now I got an OutOfMemoryError

This last one is a bad one. It went into the readFile method, found the file but never came back it just sat there for some time and blew up. This suggests there is one or more logical bugs in that method.

Anyway during debugging stack traces and System.out.println calls for general flow and tracing are your best friends.

cotton.ma at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 12

I think I found the memory bug.

while( line != null ) {

if( ( line.length() > 2 ) && ( numberLine == false ) ) {

StringTokenizer parser = new StringTokenizer( line, dels );

while( parser.hasMoreTokens() ) {

String temp = parser.nextToken();

String finalWord = temp.toLowerCase();

chapterUniques.add( finalWord );

allTheWords.add( finalWord );

}

} else {

return;

}

}

In the above code what will cause the loop to exit? What happens if it doesn't?

PS I know the answers so those are really tips

cotton.ma at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 13
Thanks so much for being so tolerant, I'll try that with stack traces and print statements and see what I come up with.
jezzica85a at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 14

I can't believe this, another problem, and I just can't figure it out. I think I've finally handled my exceptions well enough, but the problem it looks like is that the program isn't recognizing my file name. I checked mulitple times and it does exist and I'm typing it in correctly. Could I be missing something?

jezzica85a at 2007-7-14 22:38:15 > top of Java-index,Java Essentials,Java Programming...
# 15
Which problem is this? Where are getting an error/getting stuck?
cotton.ma at 2007-7-21 10:36:42 > top of Java-index,Java Essentials,Java Programming...
# 16

Jezzica sorry but I have to be going now. If you are finding the program is hanging then do checkout my post on the memory error previously. There is a loop there that never ends.

If you are past that point already I am sorry. Post the snippet of code where it is having an error or getting stuck and I am sure someone will be along to help you eventually. It is the weekend so it can be slowish...

cotton.ma at 2007-7-21 10:36:42 > top of Java-index,Java Essentials,Java Programming...
# 17

Thanks for trying Cotton, I really appreciate your help.

Just for future reference, if anyone else knows how to do this, my code seems fine, and I'm not getting any exceptions. What seems to be the problem is that even when I input a valid file name, the program tells me the file doesn't exist. I know it's not a stupid slash versus blackslash error, so I'm stumped.

jezzica85a at 2007-7-21 10:36:42 > top of Java-index,Java Essentials,Java Programming...
# 18

> Just for future reference,

Read my first comment. Create an SSCCE.

> even when I input a valid file name, the program tells me the file doesn't exist

So create an SSCCE that does exactly that. Get rid of all the extra code. Isolate the problem down to a couple of lines of code. Once you get the small program working you incorporate the concepts into your main program.

camickra at 2007-7-21 10:36:42 > top of Java-index,Java Essentials,Java Programming...
# 19

> even when I input a valid file name, the program

> tells me the file doesn't exist. I know it's not a

> stupid slash versus blackslash error, so I'm stumped.

This could happen if you input a relative path instead of an absolute path, and you are mistaken as to what you think your current working directory is.

DrClapa at 2007-7-21 10:36:42 > top of Java-index,Java Essentials,Java Programming...