Search code examples
javastruts2actionactionresultstruts-action

Action result error: "The local variable success may not have been initialized."


" Unresolved compilation problems: The local variable input may not have been initialized The local variable success may not have been initialized The local variable input may not have been initialized" Ive been trying to populate my Database with 3 inputs, I'm very new to struts2 framework at MySQl so any type of insight or input would be very helpful. Have i included enough code information or?This is my ActionClass.

<pre> package com.tutorialspoint.struts2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{

    private String osName;
    private String version;
    private String notes;



    public String execute() throws Exception {

        String input;
        String success;
        String ret = input;
        Connection conn = null;

        try{
            String URL = "jdbc:mysql://localhost/HelloWorld";
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL, "root", "");
            String sql = "SELECT osName FROM entry WHERE";
            sql+=" osName = ? AND version = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, osName);
            ps.setString(2, version);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                notes = rs.getString(1);
                ret = success;
            }
        }catch (Exception e) {
            ret = input;
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
        }

        return ret;
    }


        public String getOsName() {
        return osName;
    }
    public void setOsName(String osName) {
        this.osName = osName;
    }
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
    public String getNotes() {
        return notes;
    }
    public void setNotes(String notes) {
        this.notes = notes;
    }

    public void validate()
    {
        if (osName == null || osName.trim().equals(""))
        { 
            addFieldError("osName","The OS name is required");
        }
        if (version == null || version.trim().equals(""))
        {
            addFieldError("version","The OS version is required");
        }
    }
}

Solution

  • SUCCESS, ERROR, INPUT, NONE and LOGIN are predefined framework constants defined in the Action interface, implemented by the ActionSupport class, that your action extends.

    So, leaving apart the fact that you are not initializing your variables (what you should have done would be :

    private String success = "success";
    private String input   = "input";
    

    ), they are absolutely not needed because you can return the constant value:

    ret = SUCCESS;
    ...
    ret = INPUT;
    

    or, when not extending the ActionSupport, the literal value:

    ret = "success";
    ...
    ret = "input";
    

    The constant are preferred to avoid typos.