Using .add for arraylist doesn't seem to work, what am I doing wrong?



public boolean addPoint(Point p){ points.add(p); return points.add(p); extremes(); }

Alright so when I run this code the main class calls addPoint and passes it a Point, however when it gets to the line "points.add(p);" it gives me a "java.lang.NullPointerException" error. FYI: "points" is an arrayList.

Also on a side note, am I using the "return points.add(p);" right to return a boolean value? And on another side note I don't seem to be calling "extremes();" right, as I get an Unreachable code error.

Thanks for all the help! :)

-------------Problems Reply------------

You probably forgot to initialize your ArrayList (i.e. points). Just do something like this:

class YourClass {

private final List<Point> points = new ArrayList<Point>();

public boolean addPoint (Point p) {
boolean result = points.add(p);
extremes();
return result;
}
}

You are correct that extremes() will never get called. Either reorder the last two lines, or if the order is important then do this:

public boolean addPoint(Point p){
boolean result = points.add(p);
extremes();
return result;
}

I'd also recommend that you format your code neatly so that it is easier to read, and use more descriptive names than for example extremes. It is not clear whether this method is searching for the extremes and returning them or if you are calling it for some side-effect. If it is the former then it is strange that you are not actually using the return value - perhaps this is another error in your code.

You get an unreachable code error because your call to extremes() is AFTER the return. Your method will always return and NEVER get there.

I think what you want is

public boolean addPoint (Point p) {
boolean didAdd = points.add(p);
extremes();
return didAdd;
}

no need to call add twice.

Your NullPointerException is because points is null - did you new it in your constructor?

Unless it's your intent to stick the point in your list twice, you don't want to call points.add(p) twice. What you probably want to do is:

public boolean addPoint (Point p) {
boolean result = points.add(p);
extremes();
return result;
}

Category:java Views:0 Time:2010-09-27
Tags: java arraylist

Related post

  • How to add a Arraylist in to a specific column of DataTable? 2011-12-17

    How to add a Arraylist in to a specific column of DataTable, for example, add Arraylist of name 'mylist' to the 5th column of the DataTable name mydatatable. I try this code: foreach(string item in mylist) { mydatatable.Rows.Add(item); } But when i a

  • Add in ArrayList takes too long (more than 50000 nodes) 2011-12-08

    I have a problem. I want to create a search engine which is based on IR systems. So, I have some files, I take the information I need and I store them in structures such as HashMaps, TreeMaps, ArrayLists e.t.c. Then, I want to write this information

  • How to add one arraylist to other arraylist in android? 2011-12-29

    I have created 3 arraylists and stored data in it. I need to pass one single arraydata to other page and so I have added my individual arrays to the main array. But when I added one array to other the data is not being aded to that array. Here is my

  • Make a copy of a variable and add to Arraylist 2012-03-30

    I'm working with two-dimensional array-values that should be inserted into a ArrayList. But this is done in a for-loop and the value of the two-dimensional array-value gets changed as the loop runs since it is just used as an temp-variable (which mak

  • Getting specific Node Value from XML and add to ArrayLIst 2014-03-26

    I have an XML like this <reg> <user> <Name> abc</Name> <Email> [email protected]</Email> <picture> sdCard/imges.145.jpg </Picture> <Date> 12/12/2012</Date> </user> <user> <Name>

  • Can I add a group of ArrayList into a single ArrayList? 2010-10-28

    Can I add a group of ArrayList into a single ArrayList? The ArrayList groups are of different sizes. Then How can I recognize each ArrayList? --------------Solutions------------- I hope I understand your question correctly. An ArrayList is a list of

  • Add each time an ArrayList to the serializable file 2011-04-03

    I am not sure that i understand very well the serializable concept,my question is like that: I Have an ArrayList of class that i call History: public class History implements Serializable { private static final long serialVersionUID = 1L; String name

  • Add a list to 2D arraylist 2011-05-04

    I ve stuck with the following prob. Ignore the most code, my issue is: I have a different list x every iteration and I want to parse it in the 2DarrayList conVert so I use the conVert.add(x); but my problem is on x.clear();. When I print conVert befo

  • Add object to ArrayList at specified index 2011-09-12

    I think it's a fairly simple question, but I can't figure out how to do this properly. I've got an empty arraylist: ArrayList<object> list = new ArrayList<object>(); I've got some objects I want to add and each object has to be at a certa

  • Add Objects from one ArrayList to another 2012-01-08

    I have 9 ArrayLists and I'm trying to condense them all down into one at a given point. at the moment dotGrid is a matrix of ArrayLists, searchX and searchY simply specify which ArrayLists to add to neighbours. for(int searchX = sector[0] - 1; search

  • Add values at random index in a arraylist 2012-04-09

    I want to add some values in arraylist but at random index. Is this possible? --------------Solutions------------- You could use Math.Random with a range to do this. I doubt you want to be adding it at ANY index because ArrayList is a dynamically exp

  • 2d ArrayLists - Why does adding an object to one sublist add the same object to all sublists? 2013-03-06

    In Java, I am creating a football fixture generator for a league containing 4 teams. I have a 2d Array List called 'matchDays', which contains 6 match day Array Lists. Each match day contains 2 fixture objects. I am attempting to iterate through matc

  • Add the values on ArrayList in Android 2013-07-09

    I have develop one application. Here i have to add the value on ArrayList. if i have to click Button means that value have to add on that ArrayList. I have to click another Button means that added list is displaying. How can i do? Please give me solu

  • When to use LinkedList over ArrayList? 2008-11-27

    I've always been one to simply use: List<String> names = new ArrayList<String>(); I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code. When should LinkedList be used over A

  • Move one arraylist data to another arraylist in C# 2009-02-09

    How to move one Arraylist data to another arraylist. I have tried for many option but the output is in the form of array not arraylist --------------Solutions------------- First - unless you are on .NET 1.1, you should a avoid ArrayList - prefer type

  • HashMap(key: String, value: ArrayList) returns an Object instead of ArrayList? 2009-06-06

    I'm storing data in a HashMap with (key: String, value: ArrayList). The part I'm having trouble with declares a new ArrayList "current," searches the HashMap for the String "dictCode," and if found sets current as the returned value ArrayList. ArrayL

  • Best way to create a hashmap of arraylist 2009-06-18

    I have one million rows of data in .txt format. the format is very simple. For each row: user1,value1 user2,value2 user3,value3 user1,value4 ... You know what I mean. For each user, it could appear many times, or appear only once (you never know). I

  • Arraylist in Visual Basic .net 2009-09-21

    Can I get an example of how to make something like a Vector or an ArrayList in Visual Basic .NET? --------------Solutions------------- Dim list As New ArrayList or (equivalently): Dim list As ArrayList = New ArrayList If you want a generic List (very

  • 2d ArrayList in Java adding data 2009-11-20

    I need little help on a homework assignment. I have to create a 10 by 10 ArrayList, not an array. This is what I have and I just need a hint on how to do a for loop to add the date to the 2D ArrayList. By the way this is for putting data that are gra

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.088 (s). 11 q(s)