Note that there are some explanatory texts on larger screens.

plurals
  1. POSharedPreferences NullPointer Exception, static and non-static method
    primarykey
    data
    text
    <p>I am trying to draw a line graph using <em>afreechart</em> , I have saved the x and y points already using SharedPreferences. However, when I try to get some info from sharedpreferences i got an error, i suspect it has something to do with static and non-static methods not working together, but i cannot figure it out. </p> <p>So here's the class that where i add the x and y points:</p> <pre><code>public class DrawView extends DemoView{ static int round; static int score; public DrawView(Context context) { super(context); final AFreeChart chart = createChart(); setChart(chart); } private static XYSeriesCollection createDataset() { XYSeries xyS1 = new XYSeries("Progress", true, false); //Here I try to get the info from SharedPreferences via another class Stat t = new Stat(); round =t.getround(); for(int i = 0; i &lt; round; i++){ score = t.getscore(i); xyS1.add(i, score); } XYSeriesCollection xySC = new XYSeriesCollection(); xySC.addSeries(xyS1); return xySC; } /** * Creates a sample chart. * @param dataset the dataset. * @return A sample chart. */ private static AFreeChart createChart() { XYDataset dataset = createDataset(); AFreeChart chart = ChartFactory.createXYLineChart( "Statistics", "Rounds", "Points", dataset, PlotOrientation.VERTICAL, false, true, false); XYPlot plot = (XYPlot) chart.getPlot(); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesLinesVisible(0, true); plot.setRenderer(renderer); NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis(); domainAxis.setUpperMargin(0.2); // add some annotations... XYTextAnnotation annotation = null; Font font = new Font("SansSerif", Typeface.NORMAL, 12); annotation = new XYTextAnnotation("Progress", 96, 57); annotation.setFont(font); annotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT); plot.addAnnotation(annotation); return chart; } } </code></pre> <p>And here is the class via which I try to get info from SharePreferences :</p> <pre><code>public class Stat extends Activity { public static String filenamestat = "Stat"; SharedPreferences someStat; /** * Called when the activity is starting. * @param savedInstanceState */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); DrawView mView = new DrawView(this); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(mView); } public int getround(){ //Here is the error someStat = getSharedPreferences(filenamestat, 0); String roundtaker = someStat.getString("round", "0"); int round = Integer.parseInt(roundtaker); return round; } public int getscore(int a){ String scoretaker = Integer.toString(a); String stringscore = someStat.getString(scoretaker, "0"); int score = Integer.parseInt(stringscore); return score; } } </code></pre> <p>The LogCat</p> <pre><code>07-29 20:09:47.422: E/AndroidRuntime(414): FATAL EXCEPTION: main 07-29 20:09:47.422: E/AndroidRuntime(414): java.lang.RuntimeException: Unable to start activity ComponentInfo{at com.the.package/at com.the.package.Stat}: java.lang.NullPointerException 07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 07-29 20:09:47.422: E/AndroidRuntime(414): at android.os.Handler.dispatchMessage(Handler.java:99) 07-29 20:09:47.422: E/AndroidRuntime(414): at android.os.Looper.loop(Looper.java:123) 07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.main(ActivityThread.java:4627) 07-29 20:09:47.422: E/AndroidRuntime(414): at java.lang.reflect.Method.invokeNative(Native Method) 07-29 20:09:47.422: E/AndroidRuntime(414): at java.lang.reflect.Method.invoke(Method.java:521) 07-29 20:09:47.422: E/AndroidRuntime(414): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 07-29 20:09:47.422: E/AndroidRuntime(414): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 07-29 20:09:47.422: E/AndroidRuntime(414): at dalvik.system.NativeStart.main(Native Method) 07-29 20:09:47.422: E/AndroidRuntime(414): Caused by: java.lang.NullPointerException 07-29 20:09:47.422: E/AndroidRuntime(414): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146) 07-29 20:09:47.422: E/AndroidRuntime(414): at at com.the.package.Stat.getround(Stat.java:33) 07-29 20:09:47.422: E/AndroidRuntime(414): at at com.the.package.DrawView.createDataset(DrawView.java:50) 07-29 20:09:47.422: E/AndroidRuntime(414): at at com.the.package.DrawView.createChart(DrawView.java:71) 07-29 20:09:47.422: E/AndroidRuntime(414): at com.the.package.DrawView.&lt;init&gt;(DrawView.java:38) 07-29 20:09:47.422: E/AndroidRuntime(414): at com.the.package.Stat.onCreate(Stat.java:26) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload