Hey I have this code:

This is my main part of my program:

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;

import Fecha;



public class someclass
{

private GenericObjectPool<Fecha> fechapool;

	public boolean somefunction()
         {
              System.out.println("Im going to set set actives");
              fechapool.setMaxActive(5);
              System.out.println("set actives sets");

         }
{

It doest get to the "set actives sets" println. It prints out a Java error.

Here is my Fecha class

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class Fecha 
{
	
	public String ParsearFecha(String fecha,String formato)
	{
		formato=formato.toLowerCase();
	         System.out.println(fecha);
		
		String[] trozoTotal = fecha.split(" ");
		String[] trozoFecha = trozoTotal[0].split("-");
		String[] trozoHora = trozoTotal[2].split(":");
		String trozoSeg = trozoHora[2].substring(0, trozoHora[2].indexOf("."));
		String trozoMil = trozoHora[2].substring(trozoHora[2].indexOf(".") + 1);

	
		if (fecha.equals(null))
		{
			return null;
		}
		
		
		if ((formato.equals("año")) || (formato.equals("anio")))
		{
			return trozoFecha[0];
		}
		else if (formato.equals("mes"))
		{
			return trozoFecha[1];
		}
		else if (formato.equals("dia"))
		{
			return trozoFecha[2];
		}
		else if (formato.equals("diasemana"))
		{
			return trozoTotal[1].toUpperCase();
		}
		else if (formato.equals("horas"))
		{
			return trozoHora[0];
		}
		else if (formato.equals("minutos"))
		{
			return trozoHora[1];
		}
		else if (formato.equals("segundos"))
		{
			return trozoSeg;
		}
		else if (formato.equals("milisegundos"))
		{
			return trozoMil;
		}
		else if (formato.equals(null))
		{
			return trozoFecha[0]+"-"+trozoFecha[1]+"-"+trozoFecha[2]+" "+trozoTotal[1].toUpperCase()+" "+trozoHora[0]+":"+trozoHora[1]+":"+trozoSeg+"."+trozoMil;
		}
		else
		{
			return trozoFecha[0]+"-"+trozoFecha[1]+"-"+trozoFecha[2]+" "+trozoTotal[1].toUpperCase()+" "+trozoHora[0]+":"+trozoHora[1]+":"+trozoSeg+"."+trozoMil;
		}
		
	
	}
	
	public String CojerTiempoAhoraMismo()
	{	
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd EEE HH:mm:ss.SSS",Locale.getDefault());
		Calendar cal = Calendar.getInstance();
		return dateFormat.format(cal.getTime()).toUpperCase();
	}

}

As you see Fecha is not really complex so, not sure what it happening so if someone could explain.

When using ObjectPool and borrowObject(), it also fails there.


Any ideas/tips?

Edited 4 Years Ago by riahc3: n/a

how do you try to run it? what's in your main method? what error message do you get?
if you are trying to run one of the above classes, it will fail because there is no main method in them.

how do you try to run it? what's in your main method? what error message do you get?
if you are trying to run one of the above classes, it will fail because there is no main method in them.

My main function calls somefunction(). "Im going to set set actives" does get displayed so that part works. As soon as setmaxactive is modified or borrowobject is called, it fails.

are you getting any error messages?

Yes.........I think it was invalid calling the native method function.

I removed the pool code to the original state of the program to see if I didnt break anything along the way; The reason I cant give you the exact message but Im pretty sure it was that.

It didnt say anything explict that gave any information at all.......

found a few mistakes:

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool; 
import Fecha;
  
public class someclass
{
 
private GenericObjectPool<Fecha> fechapool;
// fechapool is not being instantiated, so it's set to the default value: null
 
	public boolean somefunction()
         {
              System.out.println("Im going to set set actives");
              fechapool.setMaxActive(5);// you're trying to run a method on
// an instance that's still null, you'll need to instantiate it first
              System.out.println("set actives sets");
 
         }
{ // doesn't this need to be a closing bracket?

found a few mistakes:

I have a stupid way of just writing code instead of copy/paste so that open bracket is closed in my code.

How do you instantiate fechapool? Like I said, havent worked with ObjectPools before...

Thanks for the help

private GenericObjectPool<Fecha> fechapool = new GenericObjectPool<Fecha>();
should do it, as far as I know, there is a default constructor in there, but never used the class myself, so I'm not an expert here neither.

The error shown is this

[img]http://img838.imageshack.us/img838/2261/errorst.png[/img]

I tried private GenericObjectPool<Fecha> fechapool = new GenericObjectPool<Fecha>(); before you posted to see if it worked and this error comes up (similar to the one before, originally in the thread)

Edited 4 Years Ago by riahc3: n/a

Attachments errors.png 96.75 KB

are you using a datospedidos class?

That is my main.

Pardon, my main is inside there.

Without ObjectPools, the code works correctly so there is nothing "strange" about it...

Edited 4 Years Ago by riahc3: n/a

according to your error message you get a NullPointerException on line 127 of the class
webservice***. check that line, add a println to test the values you use there, and check where those values are comming from. might be that method is being used by something in your ObjectPool.

Edited 4 Years Ago by stultuske: commented part of classname, request of OP

At that line is where I attempt to get the object:

// ^^^ Code before
		//Fecha t=new Fecha(); //Old way
		Fecha t=null;
		try 
/*Line 127*/		{
			System.out.println("Intento cojer del pool de fecha"); //THIS IS PRINTED
			t = fechapool.borrowObject();
			System.out.println("Ya he cojido del pool de fecha"); //THIS IS NOT PRINTED
		} 
		catch (NoSuchElementException e2) 
		{
			System.out.println(t.CojerTiempoAhoraMismo() + ": " + "Fallo en 'no such element' para el pool del objeto fecha " + e2.getLocalizedMessage());
		
		} 
		catch (IllegalStateException e2) 
		{
			System.out.println(t.CojerTiempoAhoraMismo() + ": " +"Fallo en 'illegal state exception' para el pool del objeto fecha " + e2.getLocalizedMessage());
			
		} 
		catch (Exception e2) 
		{
			System.out.println(t.CojerTiempoAhoraMismo() + ": " +"Fallo en 'exception' para el pool de fecha " + e2.getLocalizedMessage());
			
		}

Edited 4 Years Ago by riahc3: n/a

well, your problem is:

t = fechapool.borrowObject();

chances are, your fechapool is still null. could be your initialisation of fechapool is in another scope, or you've reset the value to null somewhere.

can't say for sure though

well, your problem is:

t = fechapool.borrowObject();

chances are, your fechapool is still null. could be your initialisation of fechapool is in another scope, or you've reset the value to null somewhere.

can't say for sure though

OK......

// ^^^ Code before
        //Fecha t=new Fecha(); //Old way
        Fecha t=null;
        try 
/*Line 127*/        {
            System.out.println("Intento cojer del pool de fecha"); //THIS IS PRINTED
/*Cant be null now right?*/ fechapool=new GenericObjectPool<Fecha>();
            t = fechapool.borrowObject();


            System.out.println("Ya he cojido del pool de fecha"); //THIS IS NOT PRINTED
        } 
        catch (NoSuchElementException e2) 
        {
            System.out.println(t.CojerTiempoAhoraMismo() + ": " + "Fallo en 'no such element' para el pool del objeto fecha " + e2.getLocalizedMessage());

        } 
        catch (IllegalStateException e2) 
        {
            System.out.println(t.CojerTiempoAhoraMismo() + ": " +"Fallo en 'illegal state exception' para el pool del objeto fecha " + e2.getLocalizedMessage());

        } 
        catch (Exception e2) 
        {
            System.out.println(t.CojerTiempoAhoraMismo() + ": " +"Fallo en 'exception' para el pool de fecha " + e2.getLocalizedMessage());

        }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

normally, no.
the error message you get now, does it point to the line where you instantiate that pool, or to the one where you call the borrowObject method?

your pool will be instantiated, but I think you can't call the borrowObject method yet. just read a bit in the api's of that class and that might give you an error as well.

The error happens when I call the borrowObject method. Im sure because of the println right before it (that prints out) and the println after it (which does NOT print out)

System.out.println("Intento cojer del pool de fecha"); //THIS IS PRINTED
/*Cant be null now right?*/ fechapool=new GenericObjectPool<Fecha>();
			t = fechapool.borrowObject();

if the error message is the same, it's probably the borrowObject method allright. according to the api's, that can throw an Exception if there is no object that can be borrowed.

if the error message is the same, it's probably the borrowObject method allright. according to the api's, that can throw an Exception if there is no object that can be borrowed.

And then how can I add a object to that pool so it can be borrowed?

I read the APIs and it really doesnt give me any hint as to what it could be.

I tired (one of the things mentioned) is addObject() before getting from the pool but that doesnt work either....

as I said, I never worked with it before, I never even heard of it, so I'm not an expert on the matter, but maybe this can help you out?

as I said, I never worked with it before, I never even heard of it, so I'm not an expert on the matter, but maybe this can help you out?

Was it GenericObjectPool, ObjectPool or none that you heard about at all?

Thanks for all the help stultuske. I really appreciate it.

i'm aware of the concept of a pool, just never came into contact with that package or it's classes, I'm afraid.

i'm aware of the concept of a pool, just never came into contact with that package or it's classes, I'm afraid.

This is the Apache Commons package.

Do you know any other, the "default" one with the JDK, or something alternative you are use to?

I may be able to modify my code to work with it....

This article has been dead for over six months. Start a new discussion instead.