• Post Reply Bookmark Topic Watch Topic
  • New Topic

Desktop problem  RSS feed

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I am trying to use the Desktop API to open a text file in the default text viewer. I have downloaded the demo from the java tutorial and it works fine. However, for some reason I can't figure out, I am getting the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

I have the text file in the current working directory.

I have imported the Desktop package with the following line:

I have created an instance of Desktop called desktop:

I have a button, called openTextFileButton which when pressed invokes the following code:


Could someone tell me what I am doing wrong?

Thanks in advance.
[ July 24, 2007: Message edited by: Sam Bluesman ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Bluesman:

I have imported the Desktop package with the following line:

I have created an instance of Desktop called desktop:



Both of these statements aren't quite right; one of them may be the root of your problem.

First, Desktop is a class, not a package, in java 6. The import statement above would give a "no such package" error on at least some compilers. You mean

import java.awt.Desktop;

Second, the line

private Desktop desktop;

declares a variable, but does not create an instance of Desktop. Trying to do anything with this variable except assigning to it will give a NullPointerException. You need to say something like:

private Desktop desktop = Desktop.getDesktop();

Note that getDesktop() can throw UnsupportedOperationException if the VM isn't supporting Desktops.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to find the problem:

was requried.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that up Ernest.

Sam
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!