0

I m a beginner programmer, I am developing an android application which process an image and generate its histogram. I am using AndroidPlot to draw different color channels of an image in the histogram, but I'm having some errors and exceptions.

This is my code :

public class ImgHistogram extends Activity {

ImageView ImageView;
LinearLayout ll;

ImageView imgView;
    private Bitmap source ;



private XYPlot xyPlot;
public XYSeries rSerie;

 int[] intensities={
         0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,
           101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,
           201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
 } ;    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.histo);



   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

   imgView = (ImageView) findViewById(R.id.img);

    Bundle bundle = this.getIntent().getExtras();
      source=(Bitmap) bundle.getParcelable("img");
      imgView.setImageBitmap(source);

    // initialize our XYPlot reference:
        xyPlot = (XYPlot) findViewById(R.id.xyplot);



        Bitmap image = Bitmap.createBitmap(source.getWidth(),source.getHeight(), Bitmap.Config.ARGB_8888);
        ArrayList<Integer> rVals = new ArrayList<Integer>();
        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {

        int color = image.getPixel(x, y);
       rVals.add((color >> 16) & 0xff); //red channel

            }}
        rSerie = new SimpleXYSeries(
                rVals, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "red channel");


        // Create a formatter to format Line and Point of income series
        LineAndPointFormatter format = new LineAndPointFormatter(
            Color.rgb(0, 0, 255),                   // line color
            Color.rgb(200, 200, 200),               // point color
            Color.rgb(10, 20, 20),              // fill color (none)
            null );                                    



     // add  series to the xyplot:
        xyPlot.addSeries(rSerie, format);


        // Formatting the Domain Values ( X-Axis )
        xyPlot.setDomainValueFormat(new Format() {

            private static final long serialVersionUID = 1L;

            @Override
            public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
                return new StringBuffer( intensities[ ( (Number)obj).intValue() ]  );
            }

            @Override
            public Object parseObject(String source, ParsePosition pos) {
                return null;
            }
        });

        xyPlot.setDomainLabel("");
        xyPlot.setRangeLabel("");

        // Increment X-Axis by 1 value
        xyPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1);

        xyPlot.getGraphWidget().setRangeLabelWidth(50);

        // Reduce the number of range labels
        xyPlot.setTicksPerRangeLabel(2);

        // Reduce the number of domain labels
        xyPlot.setTicksPerDomainLabel(2);

        // Remove all the developer guides from the chart
    //    xyPlot.disableAllMarkup();


    }

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }

}


and this is the stack trace:

05-23 23:52:21.731: E/AndroidRuntime(30404): FATAL EXCEPTION: main
05-23 23:52:21.731: E/AndroidRuntime(30404): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dreamaker.magipixel/com.dreamaker.magipixel.processing.ImgHistogram}: java.lang.NullPointerException
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.os.Looper.loop(Looper.java:137)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread.main(ActivityThread.java:5419)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at java.lang.reflect.Method.invokeNative(Native Method)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at java.lang.reflect.Method.invoke(Method.java:525)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at dalvik.system.NativeStart.main(Native Method)
05-23 23:52:21.731: E/AndroidRuntime(30404): Caused by: java.lang.NullPointerException
05-23 23:52:21.731: E/AndroidRuntime(30404):    at com.dreamaker.magipixel.processing.ImgHistogram.onCreate(ImgHistogram.java:110)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.Activity.performCreate(Activity.java:5372)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
05-23 23:52:21.731: E/AndroidRuntime(30404):    ... 11 more

I hope that someone could help me!

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
emy
  • 3
  • 3
  • You have a null reference exception in `line 110` in the method `onCreate` but without the line numbers of your code, we can't know where it is exactly => what's causing it. – peter May 23 '14 at 23:24
  • yes it's a nullpointerexception and this is the line 110: xyPlot.addSeries(rSerie, format); – emy May 24 '14 at 07:05

1 Answers1

0

Given the fact that in the line in question (ImgHistogram.java:110) you have only 1 object which could possibly cause the exception - xyPlot:

xyPlot.addSeries(rSerie, format);

This suggests that findViewByID returns null =< xyPlot is null. There is a question about this: findViewByID returns null the accepted answer should solve your problem:

Wait until onFinishInflate()

Community
  • 1
  • 1
peter
  • 14,348
  • 9
  • 62
  • 96
  • it didn't solve the problem, maybe the source of the problem is the variable "intensities". – emy May 24 '14 at 22:26
  • OK, can you check, if xyPlot in fact is null or not? - it should be null and in that case my solution should have worked, but if it didn't, please confirm if it is null or not. – peter May 24 '14 at 22:54
  • it shows an empty xyPlot with an ArrayIndexOutOfBoundsException exception – emy May 25 '14 at 11:47
  • it solved the problem, now you are having a different one - Create a new question for this new problem, and provide the new code and also mark the line which is throwing the new exception. – peter May 25 '14 at 13:52