This discussion is archived
5 Replies Latest reply: Nov 25, 2012 7:24 AM by 975668 RSS

java.lang.NullPointerException

975668 Newbie
Currently Being Moderated
Good morning,

I have problem with the time KeyFrame.

When i execute the program it return error java.lang.NullPointerException, I do not understand why it gives error.

I created a new program using only line of code that has the problem, and the system performed normally. Preview error.

java.lang.NullPointerException
     at gerenciadortarefas.GerenciadorController$1.handle(GerenciadorController.java:143)
     at gerenciadortarefas.GerenciadorController$1.handle(GerenciadorController.java:139)
     at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(Unknown Source)
     at com.sun.scenario.animation.shared.TimelineClipCore.playTo(Unknown Source)
     at javafx.animation.Timeline.impl_playTo(Unknown Source)
     at com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(Unknown Source)
     at javafx.animation.Animation.impl_timePulse(Unknown Source)
     at com.sun.scenario.animation.shared.AnimationPulseReceiver.timePulse(Unknown Source)
     at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(Unknown Source)
     at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(Unknown Source)
     at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
     at com.sun.javafx.tk.quantum.QuantumToolkit$8.run(Unknown Source)
     at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
     at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
     at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
     at java.lang.Thread.run(Thread.java:722)

Preview code controller:

long I_temp;
long F_temp;
private Integer segundos = 0;
long minutos;
long horas;
private DecimalFormat DuasCasas = new DecimalFormat("00");
private Timeline tl;


@FXML
private void iniciarContador(ActionEvent event){

tl = new Timeline();
tl.setCycleCount(Timeline.INDEFINITE);

tl.getKeyFrames().add(new KeyFrame(Duration.seconds(1),
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
segundos++;
lp_timeview.setText(segundos.toString());


}

}

));
tl.playFromStart();


}
  • 1. Re: java.lang.NullPointerException
    dscarminiabielefeld Newbie
    Currently Being Moderated
    Hi,
    you can use the Code-Tags, so your code is more readable.
    It's kinda hard to guess for me where the exception comes from, your code snipped doesn't really help to figure out what's wrong.
    You can take a look at http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/NullPointerException.html and probably get the bug by yourself, or post more, executable, code. It also could help if you'd tell us, what you are trying to implement.

    If the only thing you wanted to do is count every second +1 on an integer with a timeline, this works perfectly for me:
    package yourPackage;
    
    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.event.Event;
    import javafx.event.EventHandler;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    /**
     *
     * dscarminia
     */
    public class ChangingTextFonts extends Application
    {
        public static void main(String[] args)
        {
            launch(args);
        }
        
        private Timeline tl;
        public Text text2;
        
        @Override
        public void start(Stage PrimaryStage)
        {       
            tl = new Timeline();
            tl.setCycleCount(Timeline.INDEFINITE);
            
            tl.getKeyFrames().add(new KeyFrame(Duration.seconds(1), new EventHandler()
            {
                int myInt = 0;
                
                @Override
                public void handle (Event event)
                {
                   myInt++;
                   System.out.println("Text Number: " + myInt);
                }
            }));
            tl.playFromStart();
        }
    }
    Output in Console each second:
    Text Number: 1
    Text Number: 2
    Text Number: 3
    Text Number: 4
    Text Number: 5
    Text Number: 6
    Text Number: 7
    Text Number: 8
    Text Number: 9
    Text Number: 10
    Text Number: 11
    Text Number: 12
    Text Number: 13
    Text Number: 14
    Text Number: 15
    Text Number: 16
    Text Number: 17
    . . . 
    Hope this helps, greetings.

    Edited by: dscarminiabielefeld on 22-Nov-2012 01:08
  • 2. Re: java.lang.NullPointerException
    975668 Newbie
    Currently Being Moderated
    Thank you for menssage!

    My goal is to create a task manager, When you perform a task, it saves the bank the information and the time spent for the task.

    I used javafx 2 fxml for construct the task.

    I also ran the code that is giving a new fxml error separately, and it worked.

    I believe the error is easy to solve, but I can not locate it.

    The poblem in click the buttom "Iniciar"

    The following files.

    preview fullcode:

    www.monnalisa.com.br/pabx/GerenciadorTarefas.zip
  • 3. Re: java.lang.NullPointerException
    975668 Newbie
    Currently Being Moderated
    this code need of the database access. I need you help for to do the timeline in controller work.

    please.

    Thank you.
  • 4. Re: java.lang.NullPointerException
    dscarminiabielefeld Newbie
    Currently Being Moderated
    Hey,
    I think if you debug your code, you could easily get the point, where it throws the nullpointerexeception. You are trying to use or access something that is uninitialized. Have you tried to debug? If not do it and post the snipped where it crashes and all the other implemented code, that might have something to do with the crashing part.
  • 5. Re: java.lang.NullPointerException
    975668 Newbie
    Currently Being Moderated
    My friends, I close this post because it solved the error of nullpointexepition.
    It was my lack of attention in handling the scene builder.
    when I created the label did not save the id in the scene builder, but insert the controller in java. this made the system returns the error.
    thank you!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points