0

I'm working on this app for fun, it's suppose to simulate a double pendulum, but when I run the app it works for a few frames then crashes and its not clear when it crashes. This is the error code

09-04 14:07:23.047 10097-10097/com.example.doubletrouble E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.doubletrouble, PID: 10097
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
    at com.jjoe64.graphview.GridLabelRenderer.calcLabelVerticalSize(GridLabelRenderer.java:1001)
    at com.jjoe64.graphview.GridLabelRenderer.draw(GridLabelRenderer.java:1099)
    at com.jjoe64.graphview.GraphView.drawGraphElements(GraphView.java:307)
    at com.jjoe64.graphview.GraphView.onDraw(GraphView.java:336)
    at android.view.View.draw(View.java:16238)
    at android.view.View.updateDisplayListIfDirty(View.java:15235)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
    at android.view.View.updateDisplayListIfDirty(View.java:15195)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
    at android.view.View.updateDisplayListIfDirty(View.java:15195)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
    at android.view.View.updateDisplayListIfDirty(View.java:15195)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
    at android.view.View.updateDisplayListIfDirty(View.java:15195)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
    at android.view.View.updateDisplayListIfDirty(View.java:15195)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
    at android.view.View.updateDisplayListIfDirty(View.java:15195)
    at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
    at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
    at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2681)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2495)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2124)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1140)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6239)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:606)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5551)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:731)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)

Just as a heads up, I'm new to programming in the android-studio IDE. I've gone line by line through the debugger and it seems less likely to crash if I got through the debugger slowly. Maybe a memory allocation thing?


public class MainActivity extends AppCompatActivity {

    private double theta1=45*Math.PI/180;
    private double theta2=45*Math.PI/180;
    private double mass1=1;
    private double mass2=1;
    private double length1=1;
    private double length2=1;
    private double[] nextThetas= new double[2];
    private boolean on = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GraphView graph= findViewById(R.id.graph);
        // set manual X bounds
        graph.getViewport().setXAxisBoundsManual(true);
        graph.getViewport().setMinX(0);
        graph.getViewport().setMaxX(10);

        // set manual Y bounds
        graph.getViewport().setYAxisBoundsManual(true);
        graph.getViewport().setMinY(0);
        graph.getViewport().setMaxY(10);


        evolveSys();
    }

    //This function is my attempt at getting the code to update the graph after a short delay
    @Override
    protected void onResume()
    {
        super.onResume();
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                while(on)
                {
                    evolveSys();

                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {

                    }
                }
            }
        }).start();
    }


    //This function clears the graph and redraws the pendulum with the new angles
    private void evolveSys()
    {
        GraphView graph= findViewById(R.id.graph);
        graph.removeAllSeries();
        makePendulum Pendulum = new makePendulum();
        Pendulum.pendulum(graph,theta1,theta2,mass1,mass2,length1,length2);
        evolvePendulum pendulum = new evolvePendulum();
        nextThetas=pendulum.evolve(theta1,theta2,mass1,mass2,length1,length2);
        theta1=nextThetas[0];
        theta2=nextThetas[1];
    }
}

All the other classes I use in the code work fine. Let me know your thoughts and if there is a better way to execute what I'm trying to do.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
Dangatang
  • 31
  • 1
  • 4
  • Can you put a log (on Android, using Log.d) after the nextThetas=pendulum.evolve()? What is the output there? – technoplato Sep 04 '19 at 20:24
  • The output from pendulum.evolve(...) is a double array of size 2. What do you mean by log(on Android, using Log.d)? – Dangatang Sep 04 '19 at 20:34
  • 2
    Please [edit] your question to provide the complete [stack trace from the crash](https://stackoverflow.com/a/23353174). – Mike M. Sep 04 '19 at 20:41
  • @Dangatang I wasn't sure if you knew how to log to the Logcat (where you found your stacktrace) on Android. Using Log.d("MyTag", "My message") is how you do that. I understand it is a double array of size 2. I'm curious if there are actually any doubles in the array. – technoplato Sep 04 '19 at 20:44
  • Just added the full error message – Dangatang Sep 04 '19 at 21:11
  • Added the print out stuff to logcat and there always are doubles in the array. what was interesting is that even after the error occured it still printed a many more evolution of the angles. – Dangatang Sep 04 '19 at 21:19
  • The error message actually changes based on how long I make the wait time in the loop. For longer wait times it throws this extra bit java.util.ConcurrentModificationException at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573) – Dangatang Sep 04 '19 at 22:32
  • I've never used `GraphView`, but, for the stack trace in the question, you're apparently passing some null data to it somewhere. I guess that's happening in `Pendulum.pendulum()`, but we don't know what's going on there. – Mike M. Sep 05 '19 at 06:07
  • @MikeM. Thanks I'll step through that function later and see if that is where the issue is happening. – Dangatang Sep 05 '19 at 18:12
  • @ MikeM. yeah it has something to do with the GraphView package. Its just buggy and crashes inconsistently. I'm going to just try and draw the pendulum a different way and see if that helps – Dangatang Sep 05 '19 at 21:32
  • I can get it to work now for delay times >100ms but if I shorten further it crashes. I think I'm just going to try and figure out another way to create the animation. Thanks – Dangatang Sep 05 '19 at 23:29

0 Answers0