• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compile error: non-static method cannot be referenced from a static context  RSS feed

 
Will Barbee
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read several of the other posts but none are close to this one.
I have two DAO's which access tables in an SQL database. One has a constructor that returns a data entity.
The other has a constructor that returns a vector of the data entity.
The string one has no problems. The vector one causes the "non-static method" compiler error when it is used in another object.
I know I'm missing something but what?
Why does one DAO compile OK and this one not?
Thanks in advance.


==============
The first program is static and calls a program object.

=============
The program object processes data from the SQL tables and writes a .txt file.


=============
And the DAO looks like

===============



 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If DAOFChLst is not a variable, then this call is make in a static way:

But findChLst() is not declared as static:

So either declare findChLst() as static or call it from an instance of DAOFChLst.
 
Will Barbee
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response, Knute, but the answer still eludes me.

I have 8 DAO's (Data Access Objects) each of which performs reads/writes to a single table in the SQL database.
They are all very similar in that they each have:
A constructor (empty)
An add/insert to table method
A delete from table method
A read single row or read several rows (vector) method
A count the rows method

Out of all the 8 with 32 methods, why is just this ONE singled out. And why do the other three methods in the same DAO work?
I think I have overlooked a comma, a parenthesis, a bracket, something.
And two heads (or more) are better than one at finding errors.
Thanks again.

I tried to attach the programs but they do not allow .java or .txt

So here is the code:
Program Object


And the DAO


 
Will Barbee
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found it!

As I suspected, I made an error.

I had defined the DAO:
private DAOFChLst DAOCLst;

but used it as;
vecChild = DAOFChLst.findChLst(strFamNo);

That's the wrong DAO object name.
The code should have been:
vecChild = DAOCLst.findChLst(strFamNo);

This program got so confusing I drew diagram on a piece of paper to show all the SQL tables,
DAO's and Data Objects so I could keep the names straight.

Thank you all for your responses.

Now if I could just get those Vector arrays to work...
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this forum, it is preferred to post the code inline and not as an attachment, so all is good.

What problems are you having with the Vectors? Do you get an error? Why aren't you using Lists? What is the full stacktrace of the error? (You are currently printing just the message to stdout. You probably should use e.printStackTrace() to print the entire stack trace instead.)
 
Will Barbee
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Knute. As you said I put the code inline.

Regarding the vectors, I am trying to define a Vector array of 20 units.
I entered a post and it is:
http://www.coderanch.com/t/666672/java-programmer-OCPJP/certification/Generic-array-creation-compiler-error

The compiler does not like my array definition. And I have no idea why they call it "generic". To me, generic means
non-specific whereas I was very specific about what type of array it is (Vector). The error-causing code is:
and further down in the program,
which is flagged with the error.

So what is the correct coding?


 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try

Look up the constructors in the Javadocs.

Also, you should be using List instead of Vector. (There a few rare exceptions to this rule.)

Edit: A Vector is not an array, hence "[]" is not applicable. A Vector has an array under the hood, but that's an implementation detail you shouldn't have to deal with.
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector<EntFamSps> is not the same as Vector<EntFamSps>[]

Vector<EntFamSps> is a Vector of EntFamSps, but Vector<EntFamSps>[] is an array of Vectors of EntFamSps

If you don't specifically need a Vector, just use an ArrayList instead:
   List<EntFamSps> sps = new ArrayList<EntFamSps>();

If you want to set the initial capacity you can specify that in the constructor, but it isn't necessary - the List will grow automatically as needed:
   List<EntFamSps> sps = new ArrayList<EntFamSps>(20);
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!