Skip to main content
GameDev.net gamedev.net
🔒 Locked

java null pointer exception

Started by Jarwulf Jun 25, 2012 at 11:26 PM 8 replies 2.3k views
Original Post
Jarwulf
Jarwulf
Hi, while debugging I get a null pointer exception at the marked code.


private FileConvert FileConvertObject;


if (returnVal == JFileChooser.APPROVE_OPTION)
{
File selectedFile = jfileChooser.getSelectedFile();
File selectedFileHolder;

if (bamButton.isSelected()){
txtArea.setText("");
txtArea.append("Converting file format please wait\n");
txtArea.setCaretPosition(txtArea.getText().length());

selectedFileHolder = FileConvertObject.doWork(selectedFile);//EXCEPTION HERE
selectedFile = selectedFileHolder;
txtArea.append("Conversion complete\n");
txtArea.setCaretPosition(txtArea.getText().length());
}
}


The debugger doesn't make it into the function doWork as far as I can tell since I have a breakpoint inside. I dunno what's wrong since selectedFileHolder is not set to null. Is it possible that the input file is bad?
Cornstalks
Cornstalks
It doesn't look like you ever set [font=courier new,courier,monospace]FileConvertObject [/font](and you should be able to see if it's [font=courier new,courier,monospace]null [/font]or not in your debugger). You're 100% sure it's not [font=courier new,courier,monospace]null[/font]? And you're 100% sure [font=courier new,courier,monospace]selectedFile [/font]isn't [font=courier new,courier,monospace]null[/font]? Put a breakpoint on that line, and then inspect the value of everything.
andrewoid
andrewoid
Cornstalks is right, according to your code FileConvertObject is null when the exception is hit. On your first line initialise the variable and you should be fine.

Should be something like:

[source lang="java"]private FileConvert FileConvertObject = new FileConvert(argumentsRequiredForConstructor);[/source]

Either that, or make the FileConvert class static if you only ever need one instance of the class.
Jarwulf
Jarwulf
Yah you guys are right it was null. Anyways heres yet another nullpointerexception I'm not sure what to do with further down the line.

The idea is to take a piece of code and convert it from going to standard output to going to a file.



File convertedFile;


FileWriter fstream = new FileWriter(convertedFile);
BufferedWriter out = new BufferedWriter(fstream);
out.write(record.getReferenceName() + " " +
(record.getAlignmentStart() - 1) + " " + //subtract 1 to shift from one-based to zero-based
(record.getAlignmentEnd() - 1 + 1) + " " + //subtract 1 to shift from one-based to zero-based, and
// then add 1 to shift from inclusive to exclusive
record.getReadName() + " " +
record.getMappingQuality() + " " +
(record.getReadNegativeStrandFlag()? "-": "+") );
out.close();

return convertedFile;


convertedFile is null but I'm not sure how to handle it without instead using something like soandso.txt which seems a little clumsy since its immediately going to be set for further processing.
andrewoid
andrewoid
Im struggling a bit to see where your two sections of code link together, but from what I understand you need to do the following:

  1. After your initial file conversion, store the result of the conversion to a File object, or as a string.
  2. For your second bit of code, pass the File object or string as an argument to the method, then make convertedFile equal to it.

Now I THINK all you need to pass is selectedFileHolder but that depends on what FileConvertObject.doWork(selectedFile) is returning. If that is the case then just do
[source lang="java"]File convertedFile = selectedFileHolder;[/source]

For more info you can check http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FileWriter.html

On the null pointer matter, remember if you are passing a variable as an argument it will in most cases result in a nullPointerException if the variable has not been set. If your getting exceptions, check the variables of your arguments first and then see what you can do to set those.
rip-off
rip-off

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]convertedFile is null but I'm not sure how to handle it without instead using something like soandso.txt which seems a little clumsy since its immediately going to be set for further processing.[/background]

[/font]
[/quote]
This isn't a technical question - it is a requirement question. How does the user get the output of your program? Is it hard coded? Is it derived from the name of the input file? Does it overwrite the input file? Must the user specify an output location? How - via command line parameter, or do you prompt them for it?

Once you know where the user wants the file, then we get the technical questions implementing the correct behaviour.

Jarwulf
Jarwulf


[background=rgb(250, 251, 252)]convertedFile is null but I'm not sure how to handle it without instead using something like soandso.txt which seems a little clumsy since its immediately going to be set for further processing.[/background]




This isn't a technical question - it is a requirement question. How does the user get the output of your program? Is it hard coded? Is it derived from the name of the input file? Does it overwrite the input file? Must the user specify an output location? How - via command line parameter, or do you prompt them for it?

Once you know where the user wants the file, then we get the technical questions implementing the correct behaviour.
[/quote]


The actual user output is somewhere else, this function is simply intermediate processing. This is the relevant part of the original function



protected int doWork() {
IoUtil.assertFileIsReadable(INPUT);

final SFileReader reader = new SFileReader(INPUT);

Iterator<SRecord> iterator = null;

while (iterator.hasNext()) {
final SRecord record = iterator.next();
if (record.getReadUnmappedFlag()) {
continue;
}

System.out.println(record.getReferenceName() + " " +
(record.getAlignmentStart() - 1) + " " + //subtract 1 to shift from one-based to zero-based
(record.getAlignmentEnd() - 1 + 1) + " " + //subtract 1 to shift from one-based to zero-based, and
// then add 1 to shift from inclusive to exclusive
record.getReadName() + " " +
record.getMappingQuality() + " " +
(record.getReadNegativeStrandFlag()? "-": "+") );
}
reader.close();
return 0;
}


For my purposes its simply a matter of switching the output from the screen to a File since needs to be further processed.



public static File doWork(File INPUT) {

IoUtil.assertFileIsReadable(INPUT);

final SFileReader reader = new SFileReader(INPUT);
Iterator<SRecord> iterator = null;

iterator = reader.iterator();



try {
FileWriter fstream = new FileWriter(convertedFile);
BufferedWriter out = new BufferedWriter(fstream);

while (iterator.hasNext()) {
final SRecord record = iterator.next();
if (record.getReadUnmappedFlag()) {
continue;
}

out.write(record.getReferenceName() + " " +
(record.getAlignmentStart() - 1) + " " + //subtract 1 to shift from one-based to zero-based
(record.getAlignmentEnd() - 1 + 1) + " " + //subtract 1 to shift from one-based to zero-based, and
// then add 1 to shift from inclusive to exclusive
record.getReadName() + " " +
record.getMappingQuality() + " " +
(record.getReadNegativeStrandFlag()? "-": "+") );



}
out.close();
}
catch(IOException ie){
ie.printStackTrace();
}

reader.close();
return convertedFile;

}


The current problem is that computer throws a fit at


FileWriter fstream = new FileWriter(convertedFile);


because convertedFile is null. I could replace convertedFile with convertedFile.txt but I don't want to generate an outside txt file and it would be easier to simply pass convertedFile.
BitMaster
BitMaster
A FileWriter needs a valid File object because its sole purpose is to write to a file on the filesystem which is identified by the File instance. If the user cannot specify the output file and you do not want to create a temporary file then using a FileWriter is just the wrong choice. Instead, use one of the many classes which implement the Writer interface (see http://docs.oracle.c.../io/Writer.html for example).
rip-off
rip-off
Stop - you're in too deep.

Back up. Where do you want the file. Telling me that you want to use "convertedFile" is useless to me because I don't know what that variable is supposed to be, or how it might be set. I suspect you don't either - because then it would be easy to make a File object representing this location.

If this piece of code is too low level to decide where the output file should be, make the output file an additional parameter.


I could replace convertedFile with convertedFile.txt but I don't want to generate an outside txt file and it would be easier to simply pass convertedFile.
[/quote]
I don't understand this. Are you confusing variable names with file locations? If you want to create a file called "convertedFile", with new extension, you need to put that file name as a string literal in your source, rather than an unbound File reference.
Jarwulf
Jarwulf
solved
In the end I just went with

File convertedBAM = new File("convertedFile.txt");

and it works so I guess its fine then...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.