Results 1 to 8 of 8
Thread: String returning null value
- 08-02-2008, 11:27 AM #1
Member
- Join Date
- May 2008
- Posts
- 18
- Rep Power
- 0
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); }Thank you,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; } }
- 08-02-2008, 12:11 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 08-02-2008, 02:04 PM #3
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.
- 08-02-2008, 06:34 PM #4
Member
- Join Date
- May 2008
- Posts
- 18
- Rep Power
- 0
Thanks for helping me.
- 08-02-2008, 07:51 PM #5
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.
- 08-03-2008, 05:58 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 08-03-2008, 06:57 AM #7
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");
- 08-03-2008, 07:49 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
returning String from actionPerformed
By hardcore_teddy in forum New To JavaReplies: 2Last Post: 05-17-2008, 05:32 AM -
returning a value from an arraylist
By xkross in forum New To JavaReplies: 2Last Post: 04-18-2008, 05:30 PM -
Need help returning data from database
By dyn03 in forum JDBCReplies: 0Last Post: 03-11-2008, 04:55 PM -
Returning a string (Partly Bold)
By TampaTechGuy in forum New To JavaReplies: 3Last Post: 02-03-2008, 09:54 AM -
JNI - passing and returning parameters by value
By java_to_c in forum New To JavaReplies: 0Last Post: 01-31-2008, 08:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks