Guest User

Untitled

a guest
Aug 8th, 2012
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. SharedPreferences NullPointer Exception, static and non-static method
  2. public class DrawView extends DemoView{
  3.  
  4. static int round;
  5. static int score;
  6.  
  7. public DrawView(Context context) {
  8. super(context);
  9.  
  10. final AFreeChart chart = createChart();
  11.  
  12. setChart(chart);
  13. }
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. private static XYSeriesCollection createDataset() {
  21. XYSeries xyS1 = new XYSeries("Progress", true, false);
  22.  
  23. //Here I try to get the info from SharedPreferences via another class
  24.  
  25. Stat t = new Stat();
  26. round =t.getround();
  27. for(int i = 0; i < round; i++){
  28. score = t.getscore(i);
  29. xyS1.add(i, score);
  30. }
  31.  
  32.  
  33. XYSeriesCollection xySC = new XYSeriesCollection();
  34. xySC.addSeries(xyS1);
  35.  
  36.  
  37. return xySC;
  38. }
  39.  
  40.  
  41. /**
  42. * Creates a sample chart.
  43. * @param dataset the dataset.
  44. * @return A sample chart.
  45. */
  46. private static AFreeChart createChart() {
  47. XYDataset dataset = createDataset();
  48. AFreeChart chart = ChartFactory.createXYLineChart(
  49. "Statistics",
  50. "Rounds",
  51. "Points",
  52. dataset,
  53. PlotOrientation.VERTICAL,
  54. false,
  55. true,
  56. false);
  57. XYPlot plot = (XYPlot) chart.getPlot();
  58. XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
  59. renderer.setSeriesLinesVisible(0, true);
  60. plot.setRenderer(renderer);
  61. NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
  62. domainAxis.setUpperMargin(0.2);
  63.  
  64. // add some annotations...
  65. XYTextAnnotation annotation = null;
  66. Font font = new Font("SansSerif", Typeface.NORMAL, 12);
  67. annotation = new XYTextAnnotation("Progress", 96, 57);
  68. annotation.setFont(font);
  69. annotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
  70. plot.addAnnotation(annotation);
  71.  
  72.  
  73. return chart;
  74. }
  75.  
  76. public class Stat extends Activity {
  77.  
  78. public static String filenamestat = "Stat";
  79. SharedPreferences someStat;
  80. /**
  81. * Called when the activity is starting.
  82. * @param savedInstanceState
  83. */
  84. @Override
  85. public void onCreate(Bundle savedInstanceState) {
  86. super.onCreate(savedInstanceState);
  87.  
  88. DrawView mView = new DrawView(this);
  89. requestWindowFeature(Window.FEATURE_NO_TITLE);
  90. setContentView(mView);
  91.  
  92. }
  93.  
  94. public int getround(){
  95. //Here is the error
  96. someStat = getSharedPreferences(filenamestat, 0);
  97. String roundtaker = someStat.getString("round", "0");
  98. int round = Integer.parseInt(roundtaker);
  99. return round;
  100. }
  101.  
  102.  
  103. public int getscore(int a){
  104. String scoretaker = Integer.toString(a);
  105. String stringscore = someStat.getString(scoretaker, "0");
  106. int score = Integer.parseInt(stringscore);
  107. return score;
  108. }
  109.  
  110. 07-29 20:09:47.422: E/AndroidRuntime(414): FATAL EXCEPTION: main
  111. 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
  112. 07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
  113. 07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
  114. 07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
  115. 07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  116. 07-29 20:09:47.422: E/AndroidRuntime(414): at android.os.Handler.dispatchMessage(Handler.java:99)
  117. 07-29 20:09:47.422: E/AndroidRuntime(414): at android.os.Looper.loop(Looper.java:123)
  118. 07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.main(ActivityThread.java:4627)
  119. 07-29 20:09:47.422: E/AndroidRuntime(414): at java.lang.reflect.Method.invokeNative(Native Method)
  120. 07-29 20:09:47.422: E/AndroidRuntime(414): at java.lang.reflect.Method.invoke(Method.java:521)
  121. 07-29 20:09:47.422: E/AndroidRuntime(414): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  122. 07-29 20:09:47.422: E/AndroidRuntime(414): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  123. 07-29 20:09:47.422: E/AndroidRuntime(414): at dalvik.system.NativeStart.main(Native Method)
  124. 07-29 20:09:47.422: E/AndroidRuntime(414): Caused by: java.lang.NullPointerException
  125. 07-29 20:09:47.422: E/AndroidRuntime(414): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
  126. 07-29 20:09:47.422: E/AndroidRuntime(414): at at com.the.package.Stat.getround(Stat.java:33)
  127. 07-29 20:09:47.422: E/AndroidRuntime(414): at at com.the.package.DrawView.createDataset(DrawView.java:50)
  128. 07-29 20:09:47.422: E/AndroidRuntime(414): at at com.the.package.DrawView.createChart(DrawView.java:71)
  129. 07-29 20:09:47.422: E/AndroidRuntime(414): at com.the.package.DrawView.<init>(DrawView.java:38)
  130. 07-29 20:09:47.422: E/AndroidRuntime(414): at com.the.package.Stat.onCreate(Stat.java:26)
  131.  
  132. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Advertisement
Add Comment
Please, Sign In to add comment