We help IT Professionals succeed at work.

Error on loop process - FOR

Khan Consultoria
Khan Consultoria used Ask the Experts™
on
Hello guys,

I need a help on my code,

what is the correct form to do that:

        int i;
        GraphView graph = (GraphView) findViewById(R.id.graph);
        BarGraphSeries<DataPoint> series = new BarGraphSeries<>(new DataPoint[]{

                for (i=0;i<=36;i++){
                    new DataPoint(i, 36);
        }
        });
        graph.addSeries(series);

Open in new window


I am getting that message from Android Studio:

error: illegal start of expression

if I enter the code this way, it works fine:

  GraphView graph = (GraphView) findViewById(R.id.graph);
        BarGraphSeries<DataPoint> series = new BarGraphSeries<>(new DataPoint[]{
                new DataPoint(0, 36),
                new DataPoint(1, 5),
                new DataPoint(2, 3),
                new DataPoint(3, 2),
                new DataPoint(4, 6),
        });
        graph.addSeries(series);

Open in new window


I am using the lib from this link:

https://github.com/jjoe64/GraphView/wiki/Basics-of-GraphView

Regards
Alex
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
GraphView graph = (GraphView) findViewById(R.id.graph);
DataPoint[] points = new DataPoint[36];
BarGraphSeries<DataPoint> series = new BarGraphSeries<>(points);
for (int i=0;i<points.length;i++){
    points[i] = new DataPoint(i, 36);
}
graph.addSeries(series);

Open in new window

Try the above but don't use numeric literals like i did

Author

Commented:
Hello  CEHJ

I tried and it has stopped here:

BarGraphSeries<DataPoint> series = new BarGraphSeries<>(points);

it gives me an error:

java.lang.NullPointerException: Attempt to invoke interface method 'double com.jjoe64.graphview.series.DataPointInterface.getX()' on a null object reference
Top Expert 2016
Commented:
Try making line 3 the penultimate line

Author

Commented:
Yeah, you're right, it worked.

I learned how to do now, create a array of objects.

Thanks a lot for your help.

CEHJ, what is the best way to get a random number in java between 1 to 100?
Top Expert 2016

Commented: