error detials:

Java Plug-in 10.17.2.02
Using JRE version 1.7.0_17-b02 Java HotSpot(TM) Client VM
User home directory = C:\Users\dave
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
CacheEntry[http://localhost/E_COMMERCE/menu_pages/game_pages/java_game_2/Main.class]: updateAvailable=true,lastModified=Wed Mar 20 22:46:02 EDT 2013,length=6945
Exception in thread "AWT-EventQueue-6" java.lang.NullPointerException
    at dave.Main.paint(Main.java:198)
    at sun.awt.RepaintArea.paintComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

main.java

line197: public void paint(Graphics g) {
line198:      background_class.paint(g, this);
line199:      ground_class.paint(g);         
line200:      player_class.paint(g);         
.....

so i think error is saying that background_class on line198 is null. so i added a println statment

System.out.println("flag1 = "+background_class);
 background_class.paint(g, this);
 System.out.println(flag2 + "+background_class);

output:
flag1 = dave.Background@24dfb3be
flag2 = dave.Background@24dfb3be

so there is some value in it? so why does error say its null?????????

Dani AI

Generated

Observed behavior: the applet throws a NullPointerException from Main.paint when run in the browser but the same game runs fine inside Eclipse. As noted, a println that runs after the call means that particular invocation did not throw — the failing run is therefore a different execution (different applet instance, timing, or resource state) rather than the code being uniformly broken.

Common causes (browser vs IDE)

  • paint being invoked before the game objects are initialized (race between AWT thread and init/start).
  • an internal resource inside the background object (image, buffer, etc.) being null even though the background reference exists.
  • multiple applet instances, classloader/cache mismatches, or an older JAR cached by the Java plugin.
  • a static initializer throwing on plugin load (ExceptionInInitializerError will wrap the real cause; inspect the nested cause).

Practical diagnostics to add (non-invasive)

  • Log the applet instance identity and thread inside paint to confirm which instance is painting:

    System.out.println("paint this=" + System.identityHashCode(this)
        + " thread=" + Thread.currentThread().getName());
  • Guard paint so it never dereferences a possibly-uninitialized object; this prevents a hard crash and makes the real problem visible:

    Background bg = backgroundRef;
    if (bg == null) {
        g.clearRect(0,0,getWidth(),getHeight()); // safe fallback
        return;
    }
    bg.render(g, this);
  • Wrap painting logic in a try/catch for a short time to log full stack traces and the values of suspected fields (this helps show whether the NPE is on the field access or inside the background code).

Fixes and checks

  • Initialize graphics/resources earlier (constructor or init()), or set an explicit initialized flag only cleared by paint after successful init.
  • Use MediaTracker or similar to ensure images are loaded before first paint.
  • Clear the Java plugin cache and test with appletviewer to avoid cache/classloader problems.
  • If an ExceptionInInitializerError appears, read the “Caused by” section to find the underlying exception in a static initializer.

These steps reliably narrow whether the problem is a timing/race issue, a resource-loading problem inside the background class, or an instance/classloader mismatch.

Recommended Answers

All 2 Replies

I don't see a stack trace in the output you listed. If background_class.paint(g, this) caused a NullPointerException then your flag2 println would never have happened, so then you must have changed something which fixed the problem.

If the exception isn't being thrown then why are you surprised that background_class is not null?

ops forgot to say this is an japplet. and on eciplse it works fine and there are no error. that why i am able to print the value of background_class.

the error is on the webpage. when i try to load this japplet on webpade than i get this long error about line 198 being null.

so that why i add the print statments to see the values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.