0

I'm a rusty programmer, and working on an application that's going to have several object classes. My only error is a type mismatch, and I'm not sure which the debugger is reading as an object, and which one is actually in the coord class I made.

    public class Lily {
  int state;

  public Lily(coord[] pond) {
 int state = 0;
 coord place = new coord (mouseX, mouseY);
 pond = append(pond, place);   \\this is the line that's getting a type error 

  }

   public void draw(){
   ellipse(mouseX, mouseY, 40, 40);
   fill(#08BC09);

 }
}

And the coord class

public class coord {

  float[] pair = new float[2];

  public coord(float X, float Y){
    pair[0] = X;
    pair[1] = Y;

  }
}

Edit: The error message reads "Type Mismatch, "Java.lang.Object" does not match with "main.coord[]" "

Any help? Thanks in advance.

CodeMouse92
  • 6,840
  • 14
  • 73
  • 130
LittleGrandma
  • 43
  • 1
  • 6
  • http://stackoverflow.com/questions/2843366/how-to-add-new-elements-to-an-array – stark Apr 26 '16 at 00:55
  • 1
    You should edit your question and add the text of the error message. – Lii Apr 26 '16 at 00:57
  • Can you please post a [mcve] that we can run? It's hard to help debug if we can't run the code ourselves. – Kevin Workman Apr 26 '16 at 01:30
  • @stark According to the processing documentation, append "expands an array by one element and adds data to the new position." So I don't think this is an array size problem. – LittleGrandma Apr 26 '16 at 01:33
  • @KevinWorkman I'm not exactly sure what's missing from your request. The error shows up in the IDE and as a compiler error, so I can't run code from it either. I'm just trying to debug the small amount of code I've already written, but am not sure which item is being read as an Object and which is being read as the coord[]. – LittleGrandma Apr 26 '16 at 01:37
  • Is this about [tag:processing] or [tag:java]? [They're not the same thing.](http://meta.stackoverflow.com/q/321127/472647) – CodeMouse92 May 06 '16 at 23:53

1 Answers1

3

To understand the problem, please read the documentation for the append() function in the reference. Specifically, this bit:

When using an array of objects, the data returned from the function must be cast to the object array's data type. For example: SomeClass[] items = (SomeClass[]) append(originalArray, element)

In other words, you have to do this:

pond = (coord[])append(pond, place);

The reason for this is that the append() function takes an Object[] array as a parameter. So even though you pass it a coord[] array, it doesn't "know" what type the array holds. So when it returns it to you, you get an Object[] array that just happens to be a coord[] array. The compiler can't guarantee this so you get the error, but you can safely cast the array that gets returned to you.

And just a little note: you should get into the habit of using proper indentation and capitalization (classes should start with an upper-case letter), otherwise your code is a bit hard to read.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Thanks for your help! Also, the syntax note helps. My programming class is a little too far away to remember things like that. I'm studying math, so I understand the logic of programming, but a lot of the other stuff is Greek to me. – LittleGrandma Apr 27 '16 at 06:15
  • @LittleGrandma No problem, I definitely understand! – Kevin Workman Apr 27 '16 at 14:02