Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Simple solutions for the can only iterate over an array or an instance of java.lang.iterable error
Next
Answered
Brianna Anderson
  • 16
Brianna Anderson
Asked: May 18, 20222022-05-18T16:34:57+00:00 2022-05-18T16:34:57+00:00In: java

Simple solutions for the can only iterate over an array or an instance of java.lang.iterable error

  • 16

. Advertisement .

..3..

. Advertisement .

..4..

I’m trying to run a new project. I do a couple of things like this:

import com.aspose.barcode.*;
 import com.aspose.barcoderecognition.BarCodeReadType;
 import com.aspose.barcoderecognition.BarCodeReader;
 import com.aspose.words.Document;
 import com.aspose.words.DocumentBuilder;
 import com.aspose.words.ImageType;
 import com.aspose.words.NodeCollection;
 import com.aspose.words.NodeType;
 import com.aspose.words.Shape;
 
  try
  {
  // Generate barcode image
  BarCodeBuilder builder = new BarCodeBuilder();
  builder.setSymbologyType(Symbology.Code39Standard);
  builder.setCodeText("test-123");
  String strBarCodeImageSave = "img.jpg";
  builder.save(strBarCodeImageSave);
 
  // Add the image to a Word doc
  Document doc = new Document();
  DocumentBuilder docBuilder = new DocumentBuilder(doc);
  docBuilder.insertImage(strBarCodeImageSave);
  String strWordFile = "docout.doc";
  doc.save(strWordFile);
 
  // Recognition part
  // Extract image from the Word document
  NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
  int imageIndex = 0;
 
  for(Shape shape: shapes)
  { 
  if (shape.hasImage())
  {
  // If this shape is an image, extract image to file
  String extension = ImageTypeToExtension(shape.getImageData().getImageType());
  String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
  String strBarCodeImageExtracted = "" + imageFileName;
  shape.getImageData().save(strBarCodeImageExtracted);
 
  // Recognize barcode from this image
  BarCodeReader reader = new BarCodeReader((BufferedImage) Toolkit.getDefaultToolkit().getImage(strBarCodeImageExtracted),BarCodeReadType.Code39Standard);
  while (reader.read())
  {
  System.out.println("codetext: " + reader.getCodeText());
  }
  imageIndex++;
  }
  }
  }
  catch(Exception ex)
  {
  System.out.println(ex.getMessage());
  }
  }
 
  private static String ImageTypeToExtension(int imageType) throws Exception
  {
  switch (imageType)
  {
  case ImageType.BMP:
  return "bmp";
  case ImageType.EMF:
  return "emf";
  case ImageType.JPEG:
  return "jpeg";
  case ImageType.PICT:
  return "pict";
  case ImageType.PNG:
  return "png";
  case ImageType.WMF:
  return "wmf";
  default:
  throw new Exception("Unknown image type.");
  }
  }}

But in my program, I am getting the warning:

Error: can only iterate over an array or an instance of java.lang.Iterable

Can someone explain why the “ can only iterate over an array or an instance of java.lang.iterable” issue happened? Where have I gone wrong? Thank you!

arrays
  • 2 2 Answers
  • 275 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-10T02:45:43+00:00Added an answer on June 10, 2022 at 2:45 am

    The cause: This warning expressly states that you should iterate on iterable objects.

    The solution: I assume Nodecollection is probably a com.aspose.words.NodeCollection. If you want to use the foreach syntax, you should do the following:

    Node[] shapesArray = shapes.toArray();
    for (Node node : shapesArray ){ ...
    • 10
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Agathe Gallet
    2022-05-25T20:31:59+00:00Added an answer on May 25, 2022 at 8:31 pm

    Error: can only iterate over an array or an instance of java.lang.Iterable

    It is clear that it says you should only iterate on iterable objects.

    You are using a code

    NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
    ... 
    for(Shape shape: shapes)

    If the shapes base classes is an instance java.util.Collection, or java.lang.Iterable, then the for loop will fail.

    You can check if NodeCollection has a collection class that implemented java.lang.Iterable.

    Edit:

    the nodeCollection is from the com.aspose.words.

    NodeCollection implements generic Iterable directly without specifying which objects it would be handling. You should therefore generate the Iterator explicitly from the NodeCollection instance, and then iterate.

    NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
    Iterator<Shape> shapesIterator = shapes.iterator();
    ... 
    // now use the above iterator in for loop, as below
    for( Shape shape: shapesIterator )

    Refer for a similar answer so

    • 19
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.