[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

JAVA - Map

Can someone help me understand how mapping works in JAVA.  Here is a example of a problem I am having.  I have a function/method that passes in a list.

private void createXML(List selItems) {
code.......
}

Open in new window


I am trying to call this method from a button click.

sendReqButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            createXML(selItems);
           }
      });

Open in new window


So I get a error "selItems can not be resolved".  

I was told that I would need to map it.  I don't understand why
0
jkteater
Asked:
jkteater
  • 4
1 Solution
 
for_yanCommented:
In the code
createXML(selItems);
compiler expects that selItems is declared and created earlier List and wants to pass
it to the method.
If this symbol was not cdecalred and created bfore it shows sthis diagnostic
symbol cannot be resolved

It sholed be something like that:

sendReqButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
    List selItems = new ArrayLits();
   selItems.add("Book");
   selItems.add("notebook");
            createXML(selItems);
           }
      });


In such way it should work
0
 
for_yanCommented:
In genral List is collection of any objects.
In the example above I added a couple of Strings,
in some other case you can create your custom objects
and add them to the list

but the main general point - before you use any variable in Java it should be declared -
you should specify its type.

Usually on the next step you create it (initialize your object for example, or assign some value to the String)
and then you will use it
0
 
for_yanCommented:
When you declare the method, like here

private void createXML(List selItems) {
code.......
}

then compiler will assume, that at the runtime someone will
create this list and pass it as parameter to the method,
so you need just to specify the type of parameter

when you call this method somewhere in the code
you shoudl already have this entity at least declared before
and normally even already created.
Therefore if compiler didn't encounter this selItems in
createXML(selItems);
it complains with undefined symbol or something, like that


0
 
for_yanCommented:
So it is better to say that you need to define or maybe declare the variable
than to say to map it
0
 
CEHJCommented:
>>createXML(selItems);


Make 'selItems' an instance variable of the outermost class
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now