Results 1 to 18 of 18
  1. #1
    zeyt is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default NullPointerException with Java Applet

    Hi everyone, I have been developing a Java Applet game for the last 2 weeks and I'm getting a NullPointerException when I try to embed it on HTML page. Eclipse does not show up any exceptions, despite of having a Exception.printStackTrace() on every try/catch block and it works fine running as Java Applet. I have only problems with the browser, locally too (I tried both).

    Apparently, as far as I know, a NullPointerException refers to a non initialized object. If that was actually true, my code should not work either in Eclipse. I'm very desperate guys, any help would be appreacited so much. You can find my code in the following links (I uploaded code in my domain because I have several number of classes and those are a bit long):

    ("Principal.class" is my main class, which I'm using for embedding. AudioClips and Images are located together in class .jar, not accessed by System.)

    http://zeytpruebas.prosopin.com/Principal.java
    http://zeytpruebas.prosopin.com/Jugador.java
    http://zeytpruebas.prosopin.com/Enemigo.java
    http://zeytpruebas.prosopin.com/Fondo.java
    http://zeytpruebas.prosopin.com/Misiles.java
    http://zeytpruebas.prosopin.com/Misiles_PX.java
    http://zeytpruebas.prosopin.com/Pacifista.java
    http://zeytpruebas.prosopin.com/Plataforma.java

    On the other hand, Java Console's dump thread gave me this. I don't understand almost nothing so I'm not sure if will help:

    http://zeytpruebas.prosopin.com/stacktrace.txt

    And here is my embedding code for HTML:
    Java Code:
    <HTML>
     <HEAD>
      <TITLE> Franky BF38: the Iron Pirate </TITLE>
     </HEAD>
     <BODY>
      <applet code="franky_BF38/Principal.class" archive="Franky.jar" width="800" height="480"></applet>
     </BODY>
    </HTML>
    Thank you in advance for any help, and excuse gramatical mistakes.
    Last edited by zeyt; 05-03-2013 at 11:45 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NullPointerException with Java Applet

    Please provide the full error and stack trace here (not as a link).
    Also provide the relevant code, which would include the line on which it occurred in your code and where the object(s) on that line are declared and initialised.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    zeyt is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException with Java Applet

    Quote Originally Posted by Tolls View Post
    Please provide the full error and stack trace here (not as a link).
    Also provide the relevant code, which would include the line on which it occurred in your code and where the object(s) on that line are declared and initialised.
    As I said above, my code runs error-free in Eclipse so I got no reference or point to look at. If I run Java Console once I try to run applet in my webpage, the option "v" says the following:
    Java Code:
    Java Plugin 10.21.2.11
    Usando versión de JRE 1.7.0_21-b11 Java HotSpot(TM) Client VM
    Directorio raíz del usuario = C:\Users\Usuairo
    ----------------------------------------------------
    c:   borrar ventana de consola
    f:   finalizar objetos en la cola de finalización
    g:   recopilación de basura
    h:   mostrar este mensaje de ayuda
    l:   volcar lista de classloader
    m:   imprimir uso de memoria
    o:   disparar registro
    q:   ocultar consola
    r:   recargar configuración de política
    s:   volcar propiedades del sistema y de despliegue
    t:   volcar lista de threads
    v:   volcar pila de threads
    x:   borrar caché de classloader
    0-5: definir nivel de rastreo en <n>
    ----------------------------------------------------
    Volcar pila de threads...
    ----------------------------------------------------
    2013-05-03 13:47:24
    Full thread dump Java HotSpot(TM) Client VM (23.21-b01 mixed mode, sharing):
    
    "D3D Screen Updater" daemon prio=8 tid=0x0598e800 nid=0x1630 in Object.wait() [0x0681f000]
       java.lang.Thread.State: TIMED_WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	at sun.java2d.d3d.D3DScreenUpdateManager.run(Unknown Source)
    	- locked <0x28e82828> (a java.lang.Object)
    	at java.lang.Thread.run(Unknown Source)
    
    "ConsoleTraceListener" daemon prio=4 tid=0x0598c000 nid=0x1090 in Object.wait() [0x0687f000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	at java.lang.Object.wait(Object.java:503)
    	at com.sun.deploy.uitoolkit.ui.ConsoleTraceListener$ConsoleWriterThread.run(Unknown Source)
    	- locked <0x28e83340> (a com.sun.deploy.uitoolkit.ui.ConsoleTraceListener$BoundedStringBuffer)
    
    "TimerQueue" daemon prio=4 tid=0x0598d000 nid=0x108c waiting on condition [0x0692f000]
       java.lang.Thread.State: WAITING (parking)
    	at sun.misc.Unsafe.park(Native Method)
    	- parking to wait for  <0x291d9988> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    	at java.util.concurrent.locks.LockSupport.park(Unknown Source)
    	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
    	at java.util.concurrent.DelayQueue.take(Unknown Source)
    	at javax.swing.TimerQueue.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    
    "TimerQueue" daemon prio=6 tid=0x0598ec00 nid=0x890 waiting on condition [0x06caf000]
       java.lang.Thread.State: TIMED_WAITING (parking)
    	at sun.misc.Unsafe.park(Native Method)
    	- parking to wait for  <0x2903d280> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    	at java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)
    	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(Unknown Source)
    	at java.util.concurrent.DelayQueue.take(Unknown Source)
    	at javax.swing.TimerQueue.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    
    "AWT-EventQueue-1" prio=6 tid=0x0598bc00 nid=0x1738 waiting on condition [0x05abe000]
       java.lang.Thread.State: RUNNABLE
    	at com.sun.deploy.uitoolkit.ui.ConsoleHelper.dumpAllStacksImpl(Native Method)
    	at com.sun.deploy.uitoolkit.ui.ConsoleHelper.dumpAllStacks(Unknown Source)
    	at com.sun.deploy.uitoolkit.impl.awt.ui.SwingConsoleWindow$3.actionPerformed(Unknown Source)
    	at javax.swing.JComponent$ActionStandin.actionPerformed(Unknown Source)
    	at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    	at javax.swing.JComponent.processKeyBinding(Unknown Source)
    	at javax.swing.KeyboardManager.fireBinding(Unknown Source)
    	at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
    	at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
    	at javax.swing.JComponent.processKeyBindings(Unknown Source)
    	at javax.swing.JComponent.processKeyEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.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)
    
    "JVM[id=1]-Heartbeat" daemon prio=6 tid=0x0598b000 nid=0x173c in Object.wait() [0x0621f000]
       java.lang.Thread.State: TIMED_WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	at sun.plugin2.main.server.HeartbeatThread.run(Unknown Source)
    	- locked <0x28f10860> (a sun.plugin2.main.client.PluginMain$Heartbeat)
    
    "AWT-EventQueue-2" prio=4 tid=0x0598b400 nid=0x8fc waiting on condition [0x063cf000]
       java.lang.Thread.State: WAITING (parking)
    	at sun.misc.Unsafe.park(Native Method)
    	- parking to wait for  <0x28f565b8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    	at java.util.concurrent.locks.LockSupport.park(Unknown Source)
    	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
    	at java.awt.EventQueue.getNextEvent(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)
    
    "Applet 1 LiveConnect Worker Thread" prio=4 tid=0x0598a800 nid=0x970 in Object.wait() [0x057ff000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	- waiting on <0x28f10a88> (a java.lang.Object)
    	at java.lang.Object.wait(Object.java:503)
    	at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$LiveConnectWorker.run(Unknown Source)
    	- locked <0x28f10a88> (a java.lang.Object)
    	at java.lang.Thread.run(Unknown Source)
    
    "Browser Side Object Cleanup Thread" prio=6 tid=0x0598a400 nid=0x179c in Object.wait() [0x0570f000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	- waiting on <0x28f10b10> (a java.lang.ref.ReferenceQueue$Lock)
    	at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    	- locked <0x28f10b10> (a java.lang.ref.ReferenceQueue$Lock)
    	at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    	at sun.plugin2.main.client.LiveConnectSupport$BrowserSideObjectCleanupThread.run(Unknown Source)
    
    "CacheCleanUpThread" daemon prio=6 tid=0x05989c00 nid=0x17cc in Object.wait() [0x0647f000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	- waiting on <0x28f10b30> (a com.sun.deploy.cache.CleanupThread)
    	at java.lang.Object.wait(Object.java:503)
    	at com.sun.deploy.cache.CleanupThread.run(Unknown Source)
    	- locked <0x28f10b30> (a com.sun.deploy.cache.CleanupThread)
    
    "CacheMemoryCleanUpThread" daemon prio=6 tid=0x05989400 nid=0x1558 in Object.wait() [0x01d8f000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	- waiting on <0x28f10c20> (a java.lang.ref.ReferenceQueue$Lock)
    	at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    	- locked <0x28f10c20> (a java.lang.ref.ReferenceQueue$Lock)
    	at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    	at com.sun.deploy.cache.MemoryCache$LoadedResourceCleanupThread.run(Unknown Source)
    
    "MemoryCache-DelayedCleanup" daemon prio=6 tid=0x05989000 nid=0x1444 in Object.wait() [0x066bf000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	- waiting on <0x28f10cb0> (a java.util.TaskQueue)
    	at java.lang.Object.wait(Object.java:503)
    	at java.util.TimerThread.mainLoop(Unknown Source)
    	- locked <0x28f10cb0> (a java.util.TaskQueue)
    	at java.util.TimerThread.run(Unknown Source)
    
    "SysExecutionTheadCreator" daemon prio=6 tid=0x05935800 nid=0xdc4 in Object.wait() [0x064df000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	at java.lang.Object.wait(Object.java:503)
    	at sun.plugin.util.PluginSysUtil$SysExecutionThreadCreator.run(Unknown Source)
    	- locked <0x28f10cc0> (a sun.plugin.util.PluginSysUtil$SysExecutionThreadCreator)
    
    "AWT-EventQueue-0" prio=6 tid=0x0597ec00 nid=0x83c waiting on condition [0x0641f000]
       java.lang.Thread.State: WAITING (parking)
    	at sun.misc.Unsafe.park(Native Method)
    	- parking to wait for  <0x28f10d98> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    	at java.util.concurrent.locks.LockSupport.park(Unknown Source)
    	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
    	at java.awt.EventQueue.getNextEvent(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)
    
    "AWT-Windows" daemon prio=6 tid=0x0597e000 nid=0xec4 runnable [0x00f0f000]
       java.lang.Thread.State: RUNNABLE
    	at sun.awt.windows.WToolkit.eventLoop(Native Method)
    	at sun.awt.windows.WToolkit.run(Unknown Source)
    
    "AWT-Shutdown" prio=6 tid=0x0597b800 nid=0x370 in Object.wait() [0x0626f000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	- waiting on <0x28f10f00> (a java.lang.Object)
    	at java.lang.Object.wait(Object.java:503)
    	at sun.awt.AWTAutoShutdown.run(Unknown Source)
    	- locked <0x28f10f00> (a java.lang.Object)
    	at java.lang.Thread.run(Unknown Source)
    
    "Java2D Disposer" daemon prio=10 tid=0x0597b000 nid=0xb48 in Object.wait() [0x0606f000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	- waiting on <0x28f10f90> (a java.lang.ref.ReferenceQueue$Lock)
    	at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    	- locked <0x28f10f90> (a java.lang.ref.ReferenceQueue$Lock)
    	at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    	at sun.java2d.Disposer.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    
    "Java Plug-In Pipe Worker Thread (Client-Side)" daemon prio=6 tid=0x0594b800 nid=0x1784 runnable [0x05fdf000]
       java.lang.Thread.State: RUNNABLE
    	at sun.plugin2.os.windows.Windows.ReadFile0(Native Method)
    	at sun.plugin2.os.windows.Windows.ReadFile(Unknown Source)
    	at sun.plugin2.ipc.windows.WindowsNamedPipe.read(Unknown Source)
    	at sun.plugin2.message.transport.NamedPipeTransport$SerializerImpl.read(Unknown Source)
    	at sun.plugin2.message.transport.NamedPipeTransport$SerializerImpl.readByte(Unknown Source)
    	at sun.plugin2.message.AbstractSerializer.readInt(Unknown Source)
    	at sun.plugin2.message.transport.SerializingTransport.read(Unknown Source)
    	at sun.plugin2.message.Pipe$WorkerThread.run(Unknown Source)
    
    "Timer-0" prio=6 tid=0x05931000 nid=0xadc in Object.wait() [0x05e6f000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	- waiting on <0x28f112d0> (a java.util.TaskQueue)
    	at java.lang.Object.wait(Object.java:503)
    	at java.util.TimerThread.mainLoop(Unknown Source)
    	- locked <0x28f112d0> (a java.util.TaskQueue)
    	at java.util.TimerThread.run(Unknown Source)
    
    "traceMsgQueueThread" daemon prio=6 tid=0x05872800 nid=0xda0 in Object.wait() [0x05b1f000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	at java.lang.Object.wait(Object.java:503)
    	at com.sun.deploy.trace.Trace$TraceMsgQueueChecker.run(Unknown Source)
    	- locked <0x28f113b8> (a java.util.ArrayList)
    	at java.lang.Thread.run(Unknown Source)
    
    "Service Thread" daemon prio=6 tid=0x00cb3c00 nid=0x864 runnable [0x00000000]
       java.lang.Thread.State: RUNNABLE
    
    "C1 CompilerThread0" daemon prio=10 tid=0x00caf400 nid=0x15bc waiting on condition [0x00000000]
       java.lang.Thread.State: RUNNABLE
    
    "Attach Listener" daemon prio=10 tid=0x00cae000 nid=0x1094 runnable [0x00000000]
       java.lang.Thread.State: RUNNABLE
    
    "Signal Dispatcher" daemon prio=10 tid=0x00caac00 nid=0xb44 runnable [0x00000000]
       java.lang.Thread.State: RUNNABLE
    
    "Finalizer" daemon prio=8 tid=0x00c84800 nid=0x1f8 in Object.wait() [0x040df000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	- waiting on <0x28f115e0> (a java.lang.ref.ReferenceQueue$Lock)
    	at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    	- locked <0x28f115e0> (a java.lang.ref.ReferenceQueue$Lock)
    	at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    	at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)
    
    "Reference Handler" daemon prio=10 tid=0x00c82c00 nid=0x804 in Object.wait() [0x03fdf000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	at java.lang.Object.wait(Object.java:503)
    	at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
    	- locked <0x28f101b0> (a java.lang.ref.Reference$Lock)
    
    "main" prio=6 tid=0x00ced000 nid=0x894 in Object.wait() [0x00bcf000]
       java.lang.Thread.State: WAITING (on object monitor)
    	at java.lang.Object.wait(Native Method)
    	- waiting on <0x28f11668> (a sun.plugin2.message.Queue)
    	at sun.plugin2.message.Queue.waitForMessage(Unknown Source)
    	- locked <0x28f11668> (a sun.plugin2.message.Queue)
    	at sun.plugin2.message.Pipe$1.run(Unknown Source)
    	at com.sun.deploy.util.Waiter$1.wait(Unknown Source)
    	at com.sun.deploy.util.Waiter.runAndWait(Unknown Source)
    	at sun.plugin2.message.Pipe.receive(Unknown Source)
    	at sun.plugin2.main.client.PluginMain.mainLoop(Unknown Source)
    	at sun.plugin2.main.client.PluginMain.run(Unknown Source)
    	at sun.plugin2.main.client.PluginMain.main(Unknown Source)
    
    "VM Thread" prio=10 tid=0x00c81800 nid=0x89c runnable 
    
    "VM Periodic Task Thread" prio=10 tid=0x00ccb800 nid=0x1004 waiting on condition 
    
    	at sun.plugin2.main.client.PluginMain.run(Unknown Source)
    	at sun.plugin2.main.client.PluginMain.main(Unknown Source)
    
    "VM Thread" prio=10 tid=0x00c81800 nid=0x89c runnable 
    
    "VM Periodic Task Thread" prio=10 tid=0x00ccb800 nid=0x1004 waiting on condition 
    
    
    ----------------------------------------------------
    Listo.
    My main page Principal.java contains the following (rest of pages are located in links provided, If this is a trouble I can post it too but will be long):

    Java Code:
    package franky_BF38;
    
    import java.applet.Applet;
    import java.applet.AudioClip;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.URL;
    import java.util.ArrayList;
    
    public class Principal extends Applet implements Runnable, KeyListener {
    	private static final long serialVersionUID = 1L;
    
    	enum Estado {
    		Jugando, Muerto, Pausa
    	}
    
    	public static Jugador jugador;
    	public static Pacifista PX_1, PX_2;
    	public static int score = 0;
    	private Font font = new Font(getName(), Font.BOLD, 30);
    	private Image imagen = null, franky = null, franky_agachado = null, franky_salto = null, fondo = null,
    			imagen_actual = null, pacifista = null;
    	public static Image tilegrassTop = null, tilegrassBot = null, tilegrassLeft = null,
    			tilegrassRight = null, tiledirt = null, tileocean = null;
    	private Graphics graficos = null;
    	private URL direccion = null;
    	private static Fondo fondo1, fondo2;
    	private AudioClip disparo = null, tema = null;
    	public static Estado modo = Estado.Jugando;
    
    	private ArrayList<Plataforma> plataformas = new ArrayList<Plataforma>();
    
    	@Override
    	public void init() {
    		setSize(800, 480); // Tamany d'ample i alçada del Applet
    		setBackground(Color.BLACK); // Color de fons
    		setFocusable(true); // L'Applet capta l'atenció del nostre cursor; sense
    							// aquesta linia, seria necessari clicar prèviament
    							// per jugar
    		addKeyListener(this); // Permet la captació d'events de teclat
    								// (KeyListener)
    
    		Frame frame = (Frame) this.getParent().getParent(); // Assignem la
    															// finestra de
    															// l'Applet a una
    															// variable frame,
    															// posteriorment la
    															// nombrem
    		frame.setTitle("Franky BF38, the Iron Pirate");
    
    		try {
    			direccion = getDocumentBase();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		franky = getImage(direccion, "imagenes/franky_estatico.png");
    		franky_agachado = getImage(direccion, "imagenes/franky_sentado.png");
    		franky_salto = getImage(direccion, "imagenes/franky_salto.png");
    		imagen_actual = franky;
    		
    		pacifista = getImage(direccion, "imagenes/pacifista1.png");
    		
    		fondo = getImage(direccion, "imagenes/background.png");
    
    		tiledirt = getImage(direccion, "imagenes/tiledirt.png");
    		tilegrassTop = getImage(direccion, "imagenes/tilegrasstop.png");
    		tilegrassBot = getImage(direccion, "imagenes/tilegrassbot.png");
    		tilegrassLeft = getImage(direccion, "imagenes/tilegrassleft.png");
    		tilegrassRight = getImage(direccion, "imagenes/tilegrassright.png");
    
    		disparo = getAudioClip(getCodeBase(), "audio/miss.au");
    		tema = getAudioClip(getCodeBase(), "audio/franky_theme.au");
    	}
    
    	@Override
    	public void start() {
    		fondo1 = new Fondo(0, 0);
    		fondo2 = new Fondo(2160, 0);
    		jugador = new Jugador();
    
    		try {
    			cargaMapa("niveles/map2.txt");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    		PX_1 = new Pacifista(340, 360);
    		PX_2 = new Pacifista(700, 360);
    
    		tema.play();
    
    		Thread thread = new Thread(this);
    		thread.start();
    	}
    
    	private void cargaMapa(String mapa) throws IOException {
    		ArrayList<String> lineas = new ArrayList<String>();
    		int W = 0;
    		BufferedReader br = new BufferedReader(new FileReader(mapa));
    		while (true) {
    			String line = br.readLine();
    			if (line == null) {
    				br.close();
    				break;
    			}
    			if (!line.startsWith("!")) {
    				lineas.add(line);
    				W = Math.max(W, line.length());
    
    			}
    		}
    
    		for (int j = 0; j < 12; j++) { // 12
    			String line = (String) lineas.get(j);
    			for (int i = 0; i < W; i++) {
    				if (i < line.length()) {
    					char ch = line.charAt(i);
    					Plataforma t = new Plataforma(i, j,
    							Character.getNumericValue(ch));
    					plataformas.add(t);
    				}
    			}
    		}
    	}
    
    	@Override
    	public void run() {
    		if (modo == Estado.Jugando) {
    			while (true) {
    				if (modo != Estado.Pausa) {
    					try {
    						jugador.update();
    						if (jugador.isSalto()) {
    							imagen_actual = franky_salto;
    						} else if (jugador.isSalto() == false
    								&& jugador.isAgachado() == false) {
    							imagen_actual = franky;
    						}
    
    						ArrayList<Misiles> municion = jugador.getMisiles();
    						for (int i = 0; i < municion.size(); i++) {
    							Misiles m = (Misiles) municion.get(i);
    							if (m.isVisible() == true) {
    								m.update();
    							} else {
    								municion.remove(i);
    							}
    						}
    
    						ArrayList<Misiles_PX> municion_px = PX_1.getMisiles_PX();
    						for (int i = 0; i < municion_px.size(); i++) {
    							Misiles_PX m = (Misiles_PX) municion_px.get(i);
    							if (m.isVisible() == true) {
    								m.update();
    							} else {
    								municion_px.remove(i);
    							}
    						}
    
    						ArrayList<Misiles_PX> municion_px2 = PX_2.getMisiles_PX();
    						for (int i = 0; i < municion_px2.size(); i++) {
    							Misiles_PX m = (Misiles_PX) municion_px2.get(i);
    							if (m.isVisible() == true) {
    								m.update();
    							} else {
    								municion_px2.remove(i);
    							}
    						}
    
    						updatePlataformas();
    						PX_1.update();
    						PX_2.update();
    						fondo1.update();
    						fondo2.update();
    					} catch (Exception e1) {
    						// TODO Auto-generated catch block
    						e1.printStackTrace();
    					}
    					PX_1.step();
    					PX_2.step();
    
    					repaint(); // Crida a paint() i dibuixa/actualitza objectes
    								// en
    								// pantalla
    				}
    				try {
    					Thread.sleep(17); // 17 milisegons equivalen a 60FPS
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    				
    				if (jugador.getCentro_Y() > 500) { // Si es cau el jugador
    					modo = Estado.Muerto;
    				}
    			}
    		}
    	}
    
    	@Override
    	public void update(Graphics g) { // Funció pel "double buffering"
    		if (imagen == null) {
    			imagen = createImage(this.getWidth(), this.getHeight());
    			graficos = imagen.getGraphics();
    		}
    
    		graficos.setColor(getBackground());
    		graficos.fillRect(0, 0, getWidth(), getHeight());
    		graficos.setColor(getForeground());
    		paint(graficos);
    
    		g.drawImage(imagen, 0, 0, this);
    
    	}
    
    	@Override
    	public void paint(Graphics g) {
    		if (modo == Estado.Jugando) {
    			// S'ha de respectar l'ordre de linies a l'hora de pintar; en el
    			// nostre cas, si volem que en Franky estigui sobre el fons, el fons
    			// es dibuixa primer
    			g.drawImage(fondo, fondo1.getFondo_X(), fondo1.getFondo_Y(), this);
    			g.drawImage(fondo, fondo2.getFondo_X(), fondo2.getFondo_Y(), this);
    			pintaPlataformas(g);
    
    			ArrayList<Misiles> municion = jugador.getMisiles();
    			for (int i = 0; i < municion.size(); i++) {
    				Misiles m = (Misiles) municion.get(i);
    				g.setColor(Color.RED);
    				g.fillRect(m.getX(), m.getY(), 10, 5);
    			}
    
    			ArrayList<Misiles_PX> municion_px = PX_1.getMisiles_PX();
    			for (int i = 0; i < municion_px.size(); i++) {
    				Misiles_PX m = (Misiles_PX) municion_px.get(i);
    				g.setColor(Color.YELLOW);
    				g.fillRect(m.getX(), m.getY(), 10, 5);
    			}
    
    			ArrayList<Misiles_PX> municion_px2 = PX_2.getMisiles_PX();
    			for (int i = 0; i < municion_px2.size(); i++) {
    				Misiles_PX m = (Misiles_PX) municion_px2.get(i);
    				g.setColor(Color.YELLOW);
    				g.fillRect(m.getX(), m.getY(), 10, 5);
    			}
    
    			g.drawImage(pacifista, PX_1.getCentro_X() - 58,
    					PX_1.getCentro_Y() - 58, this);
    			g.drawImage(pacifista, PX_2.getCentro_X() - 58,
    					PX_2.getCentro_Y() - 58, this);
    
    			if (jugador.isAgachado() == false) {
    				g.drawRect((int) jugador.rect.getX(),
    						(int) jugador.rect.getY(),
    						(int) jugador.rect.getWidth(),
    						(int) jugador.rect.getHeight());
    				g.drawRect((int) jugador.rect3.getX(), (int) jugador.rect3.getY(),
    						(int) jugador.rect3.getWidth(),
    						(int) jugador.rect3.getHeight());
    				g.drawRect((int) jugador.rect4.getX(), (int) jugador.rect4.getY(),
    						(int) jugador.rect4.getWidth(),
    						(int) jugador.rect4.getHeight());
    			}
    			g.drawRect((int) jugador.rect2.getX(), (int) jugador.rect2.getY(),
    					(int) jugador.rect2.getWidth(),
    					(int) jugador.rect2.getHeight());
    
    			g.drawImage(imagen_actual, jugador.getCentro_X() - 61,
    					jugador.getCentro_Y() - 63, this);
    
    			g.setFont(font);
    			g.setColor(Color.WHITE);
    			g.drawString(Integer.toString(score), 740, 30);
    		} else if (modo == Estado.Muerto) {
    			g.setColor(Color.BLACK);
    			g.fillRect(0, 0, 800, 480);
    			g.setColor(Color.WHITE);
    			g.drawString("HAS PERDIDO", 300, 240);
    			tema.stop();
    		} else if (modo == Estado.Pausa) {
    			g.setColor(Color.BLACK);
    			g.fillRect(0, 0, 800, 480);
    			g.setColor(Color.WHITE);
    			g.drawString("PAUSA", 360, 240);
    		}
    	}
    
    	private void updatePlataformas() {
    
    		for (int i = 0; i < plataformas.size(); i++) {
    			Plataforma t = (Plataforma) plataformas.get(i);
    			t.update();
    		}
    	}
    
    	private void pintaPlataformas(Graphics g) {
    		for (int i = 0; i < plataformas.size(); i++) {
    			Plataforma t = (Plataforma) plataformas.get(i);
    			g.drawImage(t.getPlat_Imagen(), t.getPlataforma_X(),
    					t.getPlataforma_Y(), this);
    		}
    	}
    
    	@Override
    	public void stop() {
    		// TODO Auto-generated method stub
    		tema.stop();
    	}
    
    	@Override
    	public void destroy() {
    		// TODO Auto-generated method stub
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		switch (e.getKeyCode()) {
    		case KeyEvent.VK_UP:
    			break;
    
    		case KeyEvent.VK_DOWN:
    			imagen_actual = franky_agachado;
    			if (jugador.isSalto() == false) {
    				jugador.setAgachado(true);
    				jugador.setVelocidad_X(0);
    			}
    			break;
    
    		case KeyEvent.VK_LEFT:
    			jugador.moverIzquierda();
    			jugador.setMovimiento_Izquierda(true);
    			break;
    
    		case KeyEvent.VK_RIGHT:
    			jugador.moverDerecha();
    			jugador.setMovimiento_Derecha(true);
    			break;
    
    		case KeyEvent.VK_SPACE:
    			jugador.saltar();
    			break;
    
    		case KeyEvent.VK_CONTROL:
    			if (jugador.isAgachado() == false && jugador.isSalto() == false) {
    				jugador.disparar();
    				jugador.setPuedeDisparar(false);
    				disparo.play();
    			}
    			break;
    
    		case KeyEvent.VK_ESCAPE:
    			if (modo == Estado.Jugando)
    				modo = Estado.Pausa;
    			else
    				modo = Estado.Jugando;
    			repaint();
    			break;
    		}
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		switch (e.getKeyCode()) {
    		case KeyEvent.VK_DOWN:
    			imagen_actual = franky;
    			jugador.setAgachado(false);
    			break;
    
    		case KeyEvent.VK_LEFT:
    			jugador.stopIzquierda();
    			break;
    
    		case KeyEvent.VK_RIGHT:
    			jugador.stopDerecha();
    			break;
    
    		case KeyEvent.VK_CONTROL:
    			jugador.setPuedeDisparar(true);
    			break;
    		}
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    	}
    
    	public static Fondo getFondo1() {
    		return fondo1;
    	}
    
    	public static Fondo getFondo2() {
    		return fondo2;
    	}
    
    	public static Jugador getJugador() {
    		return jugador;
    	}
    }
    The nulls assigned to Images and Audioclips were a test trying to solve the problem, still happen the same. If you want to check my page: http://zeytpruebas.prosopin.com/index.html

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NullPointerException with Java Applet

    I don't see a null pointer in there anywhere.
    So that stack dump is not much use.
    You'll have to look into logging if you can't get hold of the stack trace.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    zeyt is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException with Java Applet

    Quote Originally Posted by Tolls View Post
    I don't see a null pointer in there anywhere.
    So that stack dump is not much use.
    You'll have to look into logging if you can't get hold of the stack trace.
    I don't know what I did but now it seems Java Console is working and showing the problem properly, here it is:

    Listo.basic: PluginMain.unregisterApplet: 3 from mananger sun.plugin2.applet.Applet2Manager@b37e25
    basic: Listener de progreso agregado: sun.plugin.util.ProgressMonitorAdapter@1630a31
    basic: Plugin2ClassLoader.addURL parent called for http://zeytpruebas.prosopin.com/Franky.jar
    network: Created version ID: 1.7.0.21
    network: Created version ID: 1.7.0.21
    basic: Applet cargado.
    basic: Se ha cambiado el tamaño del applet y agregado al contenedor principal
    basic: PERF: AppletExecutionRunnable - applet.init() BEGIN ; jvmLaunch dt 1408214 us, pluginInit dt 941784865 us, TotalTime: 943193079 us
    java.lang.NullPointerException
    at franky_BF38.Principal.init(Principal.java:57)
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter .init(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    basic: Listener de progreso eliminado: sun.plugin.util.ProgressMonitorAdapter@1630a31
    security: Restablecer el almacén de certificados de sesión denegados
    basic: Dialog type is not candidate for embedding
    Last edited by zeyt; 05-03-2013 at 02:08 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NullPointerException with Java Applet

    Too many getParent calls.
    I'm guessing a bit (though I do recall something similar many many years ago), but I suspect the Eclipse applet viewer adds an extra parent frame, whereas in a browser there's only the one level above the Applet.

    Not that I would guarantee that is actually a Frame, of course.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    zeyt is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException with Java Applet

    Quote Originally Posted by Tolls View Post
    Too many getParent calls.
    I'm guessing a bit (though I do recall something similar many many years ago), but I suspect the Eclipse applet viewer adds an extra parent frame, whereas in a browser there's only the one level above the Applet.

    Not that I would guarantee that is actually a Frame, of course.
    I deleted whole Frame stuff as it's unnecessary. Now I have an AccessControlException: access denied ("java.io.FilePermission" "niveles\map2.txt" "read") Should I sign the .jar or it is not relevant for this? Thanks for your help dude btw.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NullPointerException with Java Applet

    Yep, file system access is one of things you won't get without a signed jar.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    zeyt is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException with Java Applet

    Quote Originally Posted by Tolls View Post
    Yep, file system access is one of things you won't get without a signed jar.
    I signed my .jar following the next code:

    Java Code:
    keytool -genkey -keystore myKeyStore -alias me
    
    keytool -selfcert -keystore myKeyStore -alias me
    
    jarsigner -keystore myKeyStore jarfile.jar me
    Apparently the Applet is working fine now on my page: Franky BF38: the Iron Pirate but something is going wrong. The Applet starts with my Enum game state = Dead. I'm afraid I signed it wrong and is not reading properly my .txt which is located at the folder "niveles" inside the .jar. The only method to go GameState.Dead is falling below the ground (ground spaces) or intersecting with an enemy which is impossible as I can't move.

    Java Code:
    if (jugador.getCentro_Y() > 500) { // Si es cau el jugador
    					modo = Estado.Muerto;
    				}
    If player's y > 500 (he falls), he's dead.


    EDIT: Definately, there is no floor and that's why game insta-lose xD. How should I give the route to my .txt file? I tried ../niveles/map2.txt, map2.txt (uploading to FTP together with html file)...
    Last edited by zeyt; 05-03-2013 at 05:19 PM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NullPointerException with Java Applet

    Show how you are referring to the file in the code.
    Since the file is in the jar file you need to refer to it using getResource/getResourceAsStream.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    zeyt is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException with Java Applet

    Quote Originally Posted by Tolls View Post
    Show how you are referring to the file in the code.
    Since the file is in the jar file you need to refer to it using getResource/getResourceAsStream.
    I solved the problem yesterday. I will post the code, hope other users can use it as it's really helpfull.

    Java Code:
    URL dataStream = new URL(mapa);
    BufferedReader in = new BufferedReader(new InputStreamReader(dataStream.openStream()));
    
    //Function call:
    cargaMapa("http://zeytpruebas.prosopin.com/map2.txt");

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NullPointerException with Java Applet

    But you said the file was in the jar?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    zeyt is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException with Java Applet

    Quote Originally Posted by Tolls View Post
    But you said the file was in the jar?
    Indeed, but i wasn't able to access it. Filenotfoundexception everytime, despite of game still running but not showing the map. So right now, text file is directly uploaded to FTP together with .jar and .html files.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NullPointerException with Java Applet

    OK.
    But I suspect it was just because you were giving the wrong path, or not using getResource.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    zeyt is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException with Java Applet

    Quote Originally Posted by Tolls View Post
    OK.
    But I suspect it was just because you were giving the wrong path, or not using getResource.
    Yep. The thing is that trying new code implies new jarsigners everytime and it's kinda annoying xD

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NullPointerException with Java Applet

    That's true.
    And this way I suppose you can edit that file without having to resign/redeploy the jar file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    zeyt is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException with Java Applet

    Quote Originally Posted by Tolls View Post
    That's true.
    And this way I suppose you can edit that file without having to resign/redeploy the jar file.
    Exactly :). Now I'm working on new levels design and I'm a bit scared of how browser will respond. By the way, I noticed applet does not follow up his life-cycle if you refresh the page, I mean, if I lose my game and I refresh the page, I'm still dead. Do you know if that's proper or something?

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NullPointerException with Java Applet

    I can't remember applet life cycle stuff.
    It's been a very long time since I tried to write one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 30
    Last Post: 08-26-2011, 09:05 PM
  2. Applet NullPointerException
    By joshy150 in forum Java Applets
    Replies: 2
    Last Post: 01-13-2011, 09:05 AM
  3. applet class inside java.applet.* or java.awt.*
    By Manish87 in forum Java Applets
    Replies: 1
    Last Post: 09-27-2010, 02:15 PM
  4. Replies: 1
    Last Post: 11-25-2009, 06:48 AM
  5. ERROR: nullPointerException in applet
    By barney in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •