Solved

how to reference new class in jsp function

Posted on 2011-09-19
5
368 Views
Last Modified: 2012-08-13
I am defining a class in the body of my jsp program, and I have a list that uses this class:

class claimData {
    public String MemberId;
    public String groupId;
    public String claimNumber;
    public int count;

    public claimData(String m, String g, String c)
    {
        MemberId = m;
        groupId = g;
        claimNumber = c;
        count = 1;
    }
}

List<Object> claimList = new ArrayList<claimData>();

I also have a function that I want to use to search this list:

<%!
static void  addClaim(List<Object> claimList, String claimNumber, String memberId, String memberGroupId)
{
    // Search for retiree

    for (int i=0; i < claimList.size(); i++)
    {
        claimData cl = (claimList) list.get(i);

        if (dd.memberId.equals(memberId) && dd.groupId.equals(memberGroupId) &&
            dd.claimNumber.equals(claimNumber))
        {
            dd.count++;
            return;
        }

        claimList.add(new claimData(memberId, memberGroupId, claimNumber));
}
%>

but, the function can't see the class. I get the error: "claimData cannot be resolved to a type"

how do I fix this?
0
Comment
Question by:jmarkfoley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36563889

check the syntax in jsp - like the author of this question in the similar case (see his response below the solution):

http://stackoverflow.com/questions/2226218/defining-a-class-in-a-jsp
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 36564691
Here is an example of working code.
<%@ page import="java.util.*" %>
<%!
  class ClaimData {
    public String MemberId;
    public String groupId;
    public String claimNumber;
    public int count;

    ClaimData(String m, String g, String c)
    {
        MemberId = m;
        groupId = g;
        claimNumber = c;
        count = 1;
    }
   public String toString() 
   { return MemberId;}
 }
 void  addClaim(List<ClaimData> claimList, String claimNumber, String memberId, String memberGroupId)
{
    // Search for retiree

    for (int i=0; i < claimList.size(); i++)
    {
        ClaimData cl = (ClaimData) claimList.get(i);
       // I don't know about dd, so I commented it out for test
      //  if (dd.memberId.equals(memberId) && dd.groupId.equals(memberGroupId) &&
       //     dd.claimNumber.equals(claimNumber))
      //  {
      //      dd.count++;
       //     return;
       // }
    }
    claimList.add(new ClaimData(memberId, memberGroupId, claimNumber));
 
}
%>
<%  
  List<ClaimData> claimList = new ArrayList<ClaimData>();
  addClaim(claimList,"123","123","123");
  addClaim(claimList,"456","456","456");
%>   
<%=claimList%>

Open in new window

0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 36566981
for_yan: your program does work fine. I tried it. But when I copy/paste your exact code into my program it doesn't work. I even tried putting the class and function as the first thing in my program (rather than at the end) in case that mattered. The "body" of my jsp program doesn't even reference this class or the addClaim() function yet. I can't get the function to compile. I get the message:

An error occurred at line: 31 in the jsp file: /ERRPclaims.jsp
No enclosing instance of type ERRPclaims_jsp is accessible. Must qualify the allocation with an enclosing instance of type ERRPclaims_jsp (e.g. x.new A() where x is an instance of ERRPclaims_jsp).
28:           return;
29:       }
30:     }
31:     claimList.add(new ClaimData(memberId, memberGroupId, claimNumber));
32: }
33: %>

I can't quite figure out this "enclosing instance" business, nor why I didn't need one with your code.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 36567279
Ah ha! I figured out the problem. I had to remove the "static" from the function definition:

"void addClaim()", not "static void addClaim(). Not sure exactly why this makes a difference, but it does. Also, I meant to say that I tested using rrz@871311's code, not for_yan's.
0
 
LVL 1

Author Closing Comment

by:jmarkfoley
ID: 36567285
btw - the dd.'s were errors. They should have been cl.'s. I originally cloned the code from elsewhere.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 67
How do I remove an object from a 3 61
Java: anonymous class 4 54
maven not picking latest jar instead picking old jar from .m2 12 74
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question