473,395 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

error while using Collection class

65
what is wrong with the code below:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Class1 {
  3.  
  4.     public static void main(String[] args) {
  5.  
  6.         Collection c;
  7.         c.add("aaa");
  8.         c.add("bbb");
  9.         c.add("ccc");
  10.  
  11.     }
  12.  
  13. }
  14.  

i got this error:

java.lang.Error: Unresolved compilation problems:
The local variable c may not have been initialized
The local variable c may not have been initialized
The local variable c may not have been initialized
Jul 25 '07 #1
3 1218
JosAH
11,448 Expert 8TB
Guess what: the compiler is going to tell you that you didn't initialize a local
variable. Have a look whether or not you have initialized local variable 'c'.

kind regards,

Jos
Jul 25 '07 #2
stmfc
65
using add method i think i am initializing the local variable c,
i am new to java, in C++ vector classes are initialized in this way.
if this is not true in java, tell me how can i initialize c,and add values to store in variable c
Jul 25 '07 #3
JosAH
11,448 Expert 8TB
using add method i think i am initializing the local variable c,
i am new to java, in C++ vector classes are initialized in this way.
if this is not true in java, tell me how can i initialize c,and add values to store in variable c
Think of those Java object variables as pointers (or 'references' as they are called
in Java) to the actual objects. The pointer/reference 'c' doesn't point to an actual
object yet, i.e. it isn't initialized. You have to do something like this:

Expand|Select|Wrap|Line Numbers
  1. Collection c= new ArrayList<String>();
  2.  
Now 'c' points to an ArrayList that can store strings (similar to vector<string> in
C++). Also in C++ it isn't true that an 'add' method initializes the container.

kind regards,

Jos
Jul 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mark Buxbaum | last post by:
Hi, Is is possible to use a collection class instance as a helper object in a class? For example: MyClass.h: ---------- class MyClass {
1
by: Brian Orrell | last post by:
I know how to deserialize a collection class that is referenced in a container class as a variable or property using the XmlArray and XmlArrayItem attributes. What I can't for the life of me...
7
by: Bob | last post by:
I'm working on a Windows app that needs to write to the Registry HKLM. I keep getting a "System.UnauthorizedAccessException: Cannot write to the registry key." error when running the app. I'm...
1
by: Eric | last post by:
Using the 1.1 framework. We are using a newly created instance of a RichTextBox Class in our server code to manipulate RTF that is stored in the database. To clarify, we are not using or...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
8
by: downwitch | last post by:
Either I don't understand (entirely possible), or there's no way to copy parts of a class hierarchy from one instance to another. Say I have a class called Foo, and it contains, among other...
6
by: Erick | last post by:
I've created a class called Procs and a collection class called Processes which uses a hastable object to store the Procs. Now i want to enumerate with the "For each" to extract all the Procs in...
1
by: Bit Byte | last post by:
I was trying to put together a little utility generic collection class like so: template <class T> class Collection { public: Collection(); Collection(const Collection&); Collection&...
5
by: Nathan Sokalski | last post by:
I was trying to use a code sample from a book I bought (.NET Gotchas by O'Reilly, author = Venkat Subramaniam). The line of code I am trying to use is the following: Dim coll As New...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.