• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Desktop problem  RSS feed

 
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 ]
 
author and iconoclast
Posts: 24203
40
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!