Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Cannot infer type arguments for ArrayList<>

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot infer type arguments for ArrayList<>

    **** SOLVED *****

    Let me start by saying I did try to Google this but my interpretation is either not correct or I'm way off base, either way I need some guidance.

    At the beginning of one of my classes, I instantiate some List objects. For example ...

    private List<> byName = null;

    ... then later, I reference this again in a method like this:

    byName = new ArrayList<>(rs.getString("firstName"), rs.getString("Last Name"));

    This is where I get the error. I've tried making the list of <String>, but that didn't make a difference.

    A little help, anyone?
    Last edited by mallorz; May 1st, 2014 at 07:31 PM. Reason: Solved.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,827
    My Mood
    Mellow
    Thanks
    177
    Thanked 628 Times in 615 Posts

    Default Re: Cannot infer type arguments for ArrayList<>

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Part of the problem (maybe all?) is that is not a legal constructor for ArrayList. What are you trying to do with the parameters in the constructor?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Cannot infer type arguments for ArrayList<>

    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot infer type arguments for ArrayList<>

    Norm,

    The program won't compile with this error so there is no stack trace or any of the like available if that is what you are referring to. The full code is long and will make no sense as it is simply one tidbit of a large program with multiple classes.

    I thought the information above would be straight-forward enough for the error at hand. I assume not?

    The last statement above is underlined in red with the error 'Cannot infer type arguments for ArrayList<>' when you hover over it.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Cannot infer type arguments for ArrayList<>

    The program won't compile with this error so there is no stack trace
    I know the difference between a compiler error and a runtime error.
    My compiler generates errors. For example:

    The message shows the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    The javac command often gives better error messages than the red lines shown by an IDE.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,827
    My Mood
    Mellow
    Thanks
    177
    Thanked 628 Times in 615 Posts

    Default Re: Cannot infer type arguments for ArrayList<>

    This is correct:
    List<String> byName;
    byName = new ArrayList<>();
    What you have is not.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot infer type arguments for ArrayList<>

    I've tried that, Greg, with the same error. From how I see it, I am trying to fill the list with Strings, so it should work. Hmm.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Cannot infer type arguments for ArrayList<>

    From how I see it,... it should work
    You can't make it up. You must follow what is defined in the class and documented in the API doc.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Joining two different generic type of ArrayList.
    By Aika in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2013, 03:20 PM
  2. [SOLVED] Which is better Vector<type> or ArrayList ?
    By Andrew R in forum Java Theory & Questions
    Replies: 9
    Last Post: August 14th, 2013, 01:00 PM
  3. How to difine type each object is in a ArrayList
    By vector_ever in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 23rd, 2013, 08:24 AM
  4. [SOLVED] ArrayList.get() Returning "Incompatible Type"
    By Destro in forum Collections and Generics
    Replies: 2
    Last Post: June 15th, 2012, 06:13 PM
  5. How to perform operations on arguments with type OBJECT?
    By hexwind in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 3rd, 2011, 12:17 PM