• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getter Method for Array Objects

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

New to programming so I hope no silly questions! I'm trying to create a very simple game that comprises of a Circle class that defines the x , y coordinates along with some getter and setter methods. I have another class called TheCircles which defines a number of Circle objects to be stored in an array. I have other classes like a game engine but I don't think that's relevant. All I'm trying to do is create a method getCircle that returns a reference to a specific Circle in the array according to the parameter. So a call to getCircle(6) should return a reference to the seventh circle in the array. Does anyone know how I could this?

This is how I'm trying to call getCircle() from main.


And this is part of TheCircles class with the getCircle method.


 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch Rachel. The way I see it, you are already doing it correctly. The getter method is returning the (i)th element. The return type should be Circle and not an array of circles , Circle [] because you are supposed to return a single Circle object. At least that is how you have described your use case.
 
Rachell Zammit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks for the reply and the warm welcome

I deleted the [] in the return type of the getCircles method, however when I try running a Junit test in Eclipse a null pointer exception is being made. Any ideas where I'm going wrong?

The unit test.



 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your constructor creates an array or Circle references, but it does not create any Circle objects. When an array is created, all its elements are initially 0, false, or null, depending on the type of the array. So you either need to create Circles and put them into the array in your TheCircles constructor, or you need to do it separately after you create the TheCircles instance.
 
Rachell Zammit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes sense! Thank you that's what needed to be done

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic