Results 1 to 8 of 8
  1. #1
    impact is offline Member
    Join Date
    May 2008
    Posts
    18
    Rep Power
    0

    Default String returning null value

    I am new to java, I just dont understand why this code always returns NULL as the answer. What ever I enter the answer is the same.

    this code should basically convert "t" in the input string to "u".
    Java Code:
    public class Tool01Servlet extends HttpServlet {
       
        /** 
        * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
        * @param request servlet request
        * @param response servlet response
        */
        
        private String dnaText; // FORM dnaText
        
        
        /**
         * @param gets input from the FORM
         * Sends the data to tool()
         * Gets data from stringBean class
         * Puts data into HttpSession 
        */
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            
            dnaText = request.getParameter("dnaText");
            tool(dnaText); 
            
            stringBean stringbean = new stringBean();  
                   
            HttpSession session = request.getSession();
            session.setAttribute("answer", stringbean);
            
            String link = "answer.jsp";
            response.sendRedirect(link);
            }// End processRequest() 
    
            
            /**
             * Receives data from processRequest()
             * Sends data to sendToBean()
            */
            public void tool(String dnaText) throws IOException{
               
               try{
                   if(Pattern.matches(".*[^atgc]+.*",dnaText)){
                       String mVal = "Input sequence does not match DNA sequence";
                       sendToBean(mVal);
                    }else{
                        dnaText = dnaText.toLowerCase();   
                        dnaText = dnaText.replaceAll("t","u");
                        String mVal = dnaText;
                        sendToBean(mVal);
                    }
               }catch(Exception e){
                   sendToBean("Try catch error");
                   e.printStackTrace();
               }
            } // End tool()
            
            
            /**
             * Receives data from tool()
             * sends data to stringBean()
            */
            public void sendToBean(String mVal) throws IOException{
                 stringBean stringbean = new stringBean();
                 stringbean.stringBean(mVal);
              } //End sendToBean()
            
            
            
        /** 
        * Handles the HTTP <code>GET</code> method.
        * @param request servlet request
        * @param response servlet response
        */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        } 
    
        /** 
        * Handles the HTTP <code>POST</code> method.
        * @param request servlet request
        * @param response servlet response
        */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        }
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package dna.tool1;
    
    /**
     *
     * @author Owner
     */
    public class stringBean {
        public String message;
        
        public void stringBean(String mVal) {
        setMessage(mVal);
        }
    
        public void setMessage(String mVal){
            message = mVal;
        }    
        
        public String returnMessage(){
            return message;
        }
    }
    Thank you,

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Simply debug and see what happened there in your code. You can easily find what happened there. Seems to me you are working on NetBeans. So add a break point at the line you collect the user information. Have a try and see.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    If you can't figure out how to use interactive debug in your IDE, add a lot of System.out.println() statements to the code to see what is happening.

  4. #4
    impact is offline Member
    Join Date
    May 2008
    Posts
    18
    Rep Power
    0

    Default

    Thanks for helping me.

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    change the template to be
    if( String != null ) {
    else {// error routine }

    nulls are common in servlets if you try to do getParamater( String s )

    doing a check for null string is better than programming by exceptions.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    doing a check for null string is better than programming by exceptions.
    What's the different. I mean, if each exceptions handle in all right places no need to such a null pointer validations all the way. Depend on the implementations in some cases you have to do it, but not much. Isn't it?

  7. #7
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    You need to debug your code. In general, I first write code in a simple class that is easy to debug, and once its working, move it into the servlet world.

    "NULL" is the standard default output of a string that is null. The runtime returns the four characters N, U, L and L when you have a null string. So clearly, its got a null string. So you are not passing what you think you are.

    Use the IDE's debugger, or at least put in some PMD

    PMD == Poor Man's Debugger,
    i.e.
    System.out.println("got something here");

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, use of println() is not a good solution at all, if you don't know how to handle the IDE debugger. May be user can be confused with the println() output.

Similar Threads

  1. returning String from actionPerformed
    By hardcore_teddy in forum New To Java
    Replies: 2
    Last Post: 05-17-2008, 05:32 AM
  2. returning a value from an arraylist
    By xkross in forum New To Java
    Replies: 2
    Last Post: 04-18-2008, 05:30 PM
  3. Replies: 0
    Last Post: 03-11-2008, 04:55 PM
  4. Returning a string (Partly Bold)
    By TampaTechGuy in forum New To Java
    Replies: 3
    Last Post: 02-03-2008, 09:54 AM
  5. JNI - passing and returning parameters by value
    By java_to_c in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •